Quantcast
Channel: Dennis Robinson » Git
Viewing all articles
Browse latest Browse all 2

How to push only one commit with Git

$
0
0

The other day at work I needed to push only one commit out of several I had made locally to the remote server, for a release build.  Normally when one does a push, they push everything at once.  However, git does provide a way to push only one commit at a time.  The caveat is that the single commit you want to push must be directly above the tip of the remote branch (the oldest of your local commits).  If it is not, don’t worry as you can simply reorder your local commits to suit the situation.

SourceTree does not seem to provide any way to do what we want, but the command line interface of git does, and its actually pretty easy.  First you need to get the hash of the commit you want to push by using the command “git log” (Press “q” to exit the log).  A hash is a 40 character alpha-numeric string that looks something like 

2dc2b7e393e6b712ef103eaac81050b9693395a4
.  Once you have the correct hash, use the push command as you normally would, except provide the hash as part of the command:
$ git push <remote name> <commit hash>:<remote branch name>

# Example:
$ git push origin 2dc2b7e393e6b712ef103eaac81050b9693395a4:master

Important Note: This will push all commits up to and including the specified commit!  This means if you specify the commit that is at the top of your branch it will push everything, exactly the same as a regular push.  You need to reorder your commits first to make sure the commit you want to push is at the bottom (directly above the remote branch).  How to reorder commits with Git.

In place of the hash, you can also use standard Git revision names such as HEAD~1 or HEAD^, as well as abbreviated hash names.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images