All posts tagged SourceTree

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:

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.

There are lots of good reasons to change the order of your commits: sometimes they make more sense in a different order, or maybe you want to only push one commit out of several (more on how to do that soon).  Luckily, Git provides a way to do this, as long as the commits have not yet been pushed upstream to the remote server.  This is known as rewriting history.  I will explain how to do this with both sourcetree and git in a console.

Continue Reading