How can I push a specific commit to a remote without previous commits?

I committed the code in the local repository 2 or more times using

git commit -m "message 1" git commit -m "message 2" git commit -m "message 3" 

Now I have three commits with the following SHA

 commit-1 SHA1 commit-2 SHA2 commit-3 SHA3 

But I want to push only commit-2 in the remote repository using git push.
If I run git push, then it will push all the commits.
And I also tried the following commands:

 git push SHA2 

but it also pushed all the fixations.

How to push only this commit-2 in a remote repository?

+6
source share
2 answers

You need git rebase -i your branch first to make commit2 the first commit after origin/yourBranch .

  x--x--C1--C2--C3 (B) | (origin/B) git rebase -i C1~ x--x--C2'--C1'--C3' (B) | (origin/B) 

Then you can push this commit.

See " git - pressing a specific command :

 git push <remotename> <commit SHA>:<remotebranchname> # Example git push origin 712acff81033eddc90bb2b45e1e4cd031fefc50f:master 

It pushes all commits to and including the commit of your choice.
But since your fixation is the first, it only pushes this fixation.


I would not recommend cherry picking, as it changes the SHA1 commits: when you finally push your full branch, you will get double commits.

+6
source

You can also use cherry-pick for this, but your local branch with unclean commits will be distracted from the remote branch.

enter image description here


How to do it?

 # cd into your project folder # create new branch based on the origin branch (latest code in remote) git checkout -b <new branch> origin/master # "grab" the commit you wish to use git cherry-pick <SHA-1> # now your branch contains the desired commit. # push it back to your remote. ############################################################################ ### Note: you might not be able to push if you try to push it back to ### ### master. To fix it you might need to rename the original master ### ### and your then rename the new barch to master ### ############################################################################ 
-1
source

All Articles