Programmer Thoughts

By John Dickinson

Manipulating git commits

January 10, 2014

This evening I made a simple mistake. I made a quick patch, committed it, then submitted it for review. Then I realized I had made the commit on master and not on a separate branch. I needed to create a new branch and move the commit of of master and onto the new branch. Here’s how to fix it:

# assuming we are on master and HEAD points to the commit to move...
$ git branch my_new_branch_name  # create the branch for the commit
$ git reset --hard HEAD~1  # move the current branch back one commit

And that’s it. The commit is now safely on a new branch and i can continue to merge other commits into master or make new changes without worrying about conflicts in the new commit.

This work is licensed under a Creative Commons Attribution 3.0 Unported License.

The thoughts expressed here are my own and do not necessarily represent those of my employer.