Git: Oops! I didn’t mean to commit on that remote tracking branch!

I’ve talked about 2 very similar situations in the past. This is just a small variation, but I still find it useful enough to make another small post. I’m using the same checkout and reset commands that I’ve shown before. The small difference here, is that I’m using a remote branch as a point of reference in the reset calls. This let’s me move my local branch that is tracking the remote back to the same commit as the remote.

 

Committing To The Wrong Branch

In the first screen shot, you can see that the master branch is ahead of the origin/master remote branch.

Screen shot 2011 02 23 at 10 34 43 AM

 

Creating A Topic Branch

The first thing I need to do is create a new branch that points to the same commit as master.

Screen shot 2011 02 23 at 10 35 22 AM

Now you can see the new branch pointing to the same commit as master.

Screen shot 2011 02 23 at 10 35 39 AM

 

Resetting Master

Here, I’m using git reset to move the master branch to a different commit. In this case, I’m moving back to the same commit as the origin/master remote branch.

Screen shot 2011 02 23 at 10 36 43 AM

 

And now my repository looks the way I want. I have master and origin/master tracking nicely, and I have a topic branch that contains all of the work I had been doing.

Screen shot 2011 02 23 at 10 37 00 AM

 


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 Git. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • mt

    What if you had already pushed, how to you move the remote origin/master back to the “added empty shell…” commit?  I’m trying to do that now…can’t seem to figure out how.

    • http://mutedsolutions.com Derick Bailey

      you have to “force” the push

      git checkout master
      git reset origin/master –hard
      git push origin master -f

      the “-f” parameter is “force” and will forcefully update the remote reference to whatever ref your local branch is on.