What to do when you commit to the wrong Git branch

What to do when you commit to the wrong Git branch

What do you do when you commit to the wrong Git branch? Undo and commit to new branch, or move commits to the other branch? In this post, learn how to do both.

Share
Share
Share
Share

It’s a sunny day, you are having a great time coding, and being the good boy/girl that you are, you commit regularly.

And then it hits you: several commits, all to the wrong branch. What now?

Well, this is Git we are talking about, so the good news is that it’s relatively straightforward.

Let’s say you committed to master, and you meant to commit to a new branch called “myfeature”. There are two things that need fixing. First of all, you need to revert master back to where it originally was. And secondly, you need get your changes on that new branch.

Now here are your options:

  • Undo commits on master, checkout new branch myfeature and commit all your changes as one commit.
  • Modify refs manually for master to point back to where you were and for your new branch ref to.

The first solution is nice, simple and easy to apply without a relatively small margin for error. The downside is, however, that all you don’t get your original commit messages on the new branch and all changes are applied as a single commit with a new commit message etc.

The second solution is a little more involved. However, it means that all your commits are passed over to the new branch – though of course you should check your results before pushing up to your central repo, as it is slightly easier to get wrong.

Undo and Commit to New Branch

Make sure you are on the branch to which you have been committing. Use git log to check how many commits you want to roll back. Then undo the commits with git reset HEAD~Nwhere “N” is the number of commits you want to undo.

For example, to undo one commit:

git reset HEAD~1

Then create a new branch and check it out in one go and add and commit your changes again.

git checkout -b newbranch

git add -A

git commit -m "Committed on new branch"

Be careful with the add -A though as you may be adding unrelated uncommitted files and directories. Have a look with git status before you commit to check.

Move Commits to the Other Branch

There is more margin for error with this one. The first step is to take a note of the commit id of the commit you want to make the head of the new branch.

git log

Copy the commit id to somewhere safe.

Then reset your current branch back by one commit (or however many commits you need to go back):

git reset --hard HEAD~1

And the final step is to move the commits that follow to the new branch:

git checkout -b newbranch

git reset --hard < commit_id >

And it’s done! Time to push both branches (with --force if needed, i.e. if you had previously pushed the changes). However, as always, when using --force make sure there are no other commits that follow yours as they would be undone.

Share
Share
Share
Share

Reader Interactions

Related blog articles

    Reader Interactions