11

I committed code in 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 following SHA

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

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

git push SHA2

but this also pushed all commits.

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

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Undefined Behaviour
  • 729
  • 2
  • 8
  • 27
  • 1
    Possible duplicate of [git - pushing specific commit](http://stackoverflow.com/questions/3230074/git-pushing-specific-commit) – Andrew C Jan 18 '16 at 20:14

2 Answers2

12

You need to git rebase -i your branch first, in order 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 that commit.

See "git - pushing specific commit":

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

It does push all commits up to and including the commit you choose.
But since your commit is the first one, it only pushes that commit.


I would not recommend cherry-picking, as it changes the SHA1 of the commits pushed: when you will eventually push your full branch, you will end up with duplicate commits.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Hey, This is not working at my site,I have made 5 commits at local, I have set my head to c3. When I have tried to push c3, along with c3, c1 and c2 are also pushed. I want only c3 has to be pused on to remote server, not c1 and c2. – Maaz Patel Sep 28 '17 at 11:33
  • @MaazPatel That means you did not rebase as I described in the answer. If you did, `C3` would not have `C2` and `C1` as parent. You would order your commit so that the commit (`C3` in your case, `C2` in the case of my answer) you want to push has `origin/B` as parent. – VonC Sep 28 '17 at 11:37
-1

You can also use cherry-pick to do it but your local branch with the unpushed commits will divert from your 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                   ###
############################################################################
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
CodeWizard
  • 128,036
  • 21
  • 144
  • 167