Oops, I just pushed a git commit to the wrong branch. What now?

Nick Raphael - Jan 23 '20 - - Dev Community

We are currently running off two active branches in our git repository. And by accident I just committed an update on the wrong branch and pushed it. Luckily, getting out of this mess isn't hard.

The erroneous commit is 65c356c8c

First, lets pull the commit from Branch1 into Branch2 where it should be...

   git checkout Branch2 
   git cherry-pick 65c356c8c
Enter fullscreen mode Exit fullscreen mode

This pulls the commit across into the correct branch. Great. We can now push the commit is normal. But what about the commit still sitting on Branch1 in my local index? Lets fix that now.

   git checkout Branch1
   git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

This will reverse the last commit.

As with all git issues, the secret is to not panic. Jump on the net and find a simple solution.

. . . . . . . . . . . . . . . . . . . . . . . . . .