Git: D’oh! I Meant To Create A New Branch First!

Yesterday I ran into a situation with git where I was working away on some code, finished what I was doing and committed the changes. Immediately after making the commit, I realized that I was still sitting in my master branch instead and had made the commits there instead of on a topic branch like I meant to.

image

If I were using subversion, at this point I would create a branch from the head of where I committed and then revert the original commits. The subversion history would show that I made the mistake and anyone that pulled changes from the original location would get the commits applied and then rolled back.

Git makes this situation trivial, and no one will ever have to know that you accidentally made a commit to the wrong branch, first. :)

 

Git Reset To The Rescue!

The first thing you want to do is snap off a new branch, just like you would in subversion. I normally like to use the ‘checkout’ shortcut to create the branch and switch to the branch at the same time:

   1: git checkout -b mytopicbranch

but in this case, I don’t actually want to checkout the branch yet, so I’m just going to create a branch without switching to it:

   1: git branch mytopicbranch

At this point, both mytopicbranch and master are pointing to the same location.

image

What we really want, though, is for master to still be back at “initial commit” and mytopicbranch to contain the two commits that were made after that. To do this, we need to know the sha fingerprint of the commit we want (which we can see in the above image) and we need to use the ‘git reset’ command. The great thing about reset is that is doesn’t do anything to the contents of the repository like a revert would do. Rather, the reset command just moves the branch pointer (the HEAD of the branch) to the commit that you specify.

Run this command:

   1: git reset 2f7efb32 --hard

and you will see git move the HEAD of the master branch to that commit:

image

Now when we look at the repository again, we see master where we want it and we see mytopicbranch where it should be, as well!

image

No changes were made to the content of the repository. We only moved the HEAD pointer for the master branch around and made it look like we actually created our branch before making those other two commits. :)


Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs SignalLeaf.com - the amazingly awesome podcast audio hosting service that everyone should be using, and WatchMeCode.net where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in Branch-Per-Feature, Command Line, Git, Source Control. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Jason Watts

    Derik,

    I appreciate all your posts. They always seem to be topics that are relevant to my development life.

    Seeing you ‘accidentally’ work on the master branch reminded me of this link :

    http://nvie.com/git-model

  • http://www.lostechies.com/members/derick.bailey/default.aspx derick.bailey

    thanks jason! and yes, that’s a great post. i’ve read through that a number of times and often refer people to it when they are trying to learn how to do collaborative development with git. :)

  • Chris Ortman

    You don’t need the SHA, git reset –hard HEAD~1 (you can replace 1 with however many commits you want to count back)

  • Stephenjchan

    Thank you, this is exactly what I was looking for.  I make this mistake a lot (accidentally committing in master), and I really hate the complications that can come up when I try to merge my changes in master with changes fetched from the origin.

  • http://www.register-web-domain.in/ Domain registration

    Very useful information 

  • http://twitter.com/MarchyB Marcel Bradea

    Great writeup. Helped a lot! And very clear with the pictures :)

  • Ian Vaughan

    or :
    git branch mytopicbranch
    git reset HEAD~1 –hard

  • http://www.jimthechimp.co.uk/ James Linnell

    Nice write up, thanks