0

How do I push a single commit to the repo? specificially the one on top 388c85. When I try to push to remote repo it picks the two commits below.

Git Log

commit 388c85ccb56164c6f0c91584aa9251b87acc7f89
Author: test <test@gmail.com>
Date:   Mon Jun 6 16:38:50 2016 -0400

Login Page updates

commit 3d4b4da976bfefff4a18bfbcbfb9bdaa35d7c991
Author: test <test@gmail.com>
Date:   Mon Jun 6 13:09:19 2016 -0400

initial commit this mostly include user interface updates
gturri
  • 13,807
  • 9
  • 40
  • 57
tom_cruz
  • 411
  • 1
  • 6
  • 15
  • I have already tried this "git push -u origin 388c85ccb56164c6f0c91584aa9251b87acc7f89:BES-22" – tom_cruz Jun 10 '16 at 05:22
  • got an error "error: unable to push to unqualified destination: BES-22 The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. " – tom_cruz Jun 10 '16 at 05:22
  • `git cherry-pick` is what you need. `git rebase` also works but is not so easy to understand. – ElpieKay Jun 10 '16 at 06:31

3 Answers3

2

As far as I understand, you have something like this:

A -- B -- C -- D -- E
     \               \- master
      \- origin/master

and you want to add commit E to your remote repository, but you don't want C and D.

Then you should cherry-pick E:

git checkout B # use for instance the sha1 of this commit
git checkout -B BES-22 # it creates the branch BES-22 locally and puts you on it
git cherry-pick E

At this point you have:

A -- B -- C -- D -- E
      \ -- E'       \-master
       \    \- BES-22
        \- origin/master

So you can push:

git push origin BES-22

In particular, note that:

  • Since commits are immutable in Git, the cherry-pick created a brand new commit E'

  • If you have the error error: unable to push to unqualified destination: BES-22 The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref., it more or less means that branch BES-22 doesn't exist on the remote. Using git push origin <branch> instead of git push origin <sha1>:<unexisting remote branch> should fix it

gturri
  • 13,807
  • 9
  • 40
  • 57
0

try below command

git push <remotename> <commit SHA>:<remotebranchname>

Example

git push origin 388c85ccb56164c6f0c91584aa9251b87acc7f89:master

In above case master is your branch to push.

Amol Udage
  • 2,917
  • 19
  • 27
  • I have tried this git push origin 388c85ccb56164c6f0c91584aa9251b87acc7f89:BES-22, but it pushes the two commits instead of one – tom_cruz Jun 10 '16 at 06:02
0

git always pushes all the commits upto the HEAD (Given commit hash). So in your case when you do git push all the commits prior to the HEAD are pushed and even if you do git push origin 388c85ccb56164c6f0c91584aa9251b87acc7f89:master it would push all commits upto 388c85ccb56164c6f0c91584aa9251b87acc7f89. One thing you can do is reorder your commit history so that the history looks like below:

commit 3d4b4da976bfefff4a18bfbcbfb9bdaa35d7c991
Author: test <test@gmail.com>
Date:   Mon Jun 6 13:09:19 2016 -0400

initial commit this mostly include user interface updates

commit 388c85ccb56164c6f0c91584aa9251b87acc7f89
Author: test <test@gmail.com>
Date:   Mon Jun 6 16:38:50 2016 -0400

Login Page updates

Now if you do git push origin 388c85ccb56164c6f0c91584aa9251b87acc7f89:master only 388c85ccb56164c6f0c91584aa9251b87acc7f89 commit will be pushed and not 3d4b4da976bfefff4a18bfbcbfb9bdaa35d7c991

In order to re-order git commits, you can Refer: https://stackoverflow.com/a/16203754/3878940 or https://stackoverflow.com/a/4981173/3878940

Community
  • 1
  • 1
Aditya Singh
  • 15,810
  • 15
  • 45
  • 67