How to fix that mistake commit you just pushed to your git repository

I’ve already written a time or two how you can use git rebase to change your commit history as you work. Handy handy.. I think everyone should know how to do that.

Next up.. what if you pushed a commit and realize it’s not so great. First of all, everyone discourages this of course. The encouraged way is to just make another commit that fixes what you want different. We all want to break that rule every now and then though. Here’s how…

1) Fix your current branch (usually master).
Use the commit –amend or rebase to make it the way you want.
2) Remove the remote master

> git push origin :master

3) Push your rewritten master back to origin.

> git push origin master

OK, newer versions of git deny deleting the current branch. Just set a property to allow that first then you’re good to go.

> #ssh/cd or whatever to origin repository
> git config receive.denyDeleteCurrent ignore

Lastly, what if someone actually pulled your changes after you pushed them? They just need to reset to an earlier commit and pull again

> git reset --hard HEAD^ # or whatever sha or previous revision that was common.
> git pull # gets the latest version with the corrected commit now.

Have fun.

This entry was posted in Programming and tagged , , , . Bookmark the permalink.