How to nuke a git commit on the remote repo

Here is the situation I ran into this morning. I was happily writing code, periodically pulling changes from GitHub, like a good developer that I am. When I was finally ready to push, I received a merge from GitHub that deleted most of my Django project files. Turns out that one of the other developer on the team accidentally deleted those files and committed it locally. He didn’t push all the commits up to GitHub until just before I tried to push mine (hence I didn’t get any merges from him all morning).

So now I’m in this state where my local repo has a number of commits that I need to push up. But I can’t because I don’t want to merge in all the accidental deletes. I need to remove/fix the deletion first before I can push. How do I do that? After a few minutes on Google, I got the general approach of using git rebase but nothing that points me to actual steps that I think will fix my problem. Then I turned to Twitter, posted a question, and waited while I did some more googling. Low and behold, I received tips from @XTZGZoReX on a possible approach and after I tried it out, it works perfectly!

Here is how:

  1. Leave the existing local repo alone. You may want to reset it back to the pre-merge state by doing git reset --hard
  2. Make a fresh clone of the repo from GitHub. Let’s call it repo2.
  3. Perform an interactive rebase on repo2 and set the starting point to one commit before the one you want to nuke: git rebase -i
  4. The interactive rebase will open up a text editor (vi if you haven’t changed the default configuration) with a list of all the commits starting from the commit you want to nuke to the latest.
  5. Remove the line with the commit you want to nuke (‘dd’ keys in vi) and save the file (‘:wq’ in vi)
  6. Git will now try to apply all the subsequent commits and roll forward to the latest state. In my case, I needed to manually add and remove some files on some of the commits. After each git add/remove, you need to tell git to continue the rebase: git rebase --continue
  7. When all the commits are roll-forwarded by git, push it up to GitHub by doing a force push: git push -f
  8. Now, switch back to your original repo and pull the latest from GitHub. Good chance that you will get merge conflicts. Resolve, commit, and push. And you are done!

How safe do you feel?

Most people I know do not have any plan to backup their data. Somehow they put their trust to a piece of 3.5″ glass disc, spinning at 5400 times a second or more with a sharp metal spike fraction of a hair above it. Me? I like to spread my risk and backup my data, especially after suffering a total data lost about 12 years ago. As Alex Lindsay from the PixelCorps often says on podcasts, “Unless the data is stored in 3 different places, it doesn’t exist”. This may sound over the top but with all the online file sharing or storage services available it is actually pretty easy to have decent backup strategy with minimum cost. To achieve comprehensive backup coverage, it would definitely cost a few dollars.

Like computer security, any backup strategy should be constructed in layers. This applies to both time, location, and accessibility. Let me use my backup strategy as an example.

Continue reading “How safe do you feel?”

Website Built with

Up ↑

%d bloggers like this: