Two commits ago, I accidently changed a free picture with a picture which is protected by law (well I am not allowed to distribute that image). I haven’t pushed to github yet. How can I undo these changes made to the picture, so that it doesn’t show up in the commit history? As if this picture was never changed.
Share
Use
git rebase -i HEAD~2. When you’re prompted for what you want to do, for the commit you need to change, replace “pick” with “edit”. This will allow you to edit the commit you specify, and that means edit the entire commit, not just the message.(Or, if you don’t need that commit at all, just delete the line containing the bad commit. The rebase will finish and make it so that commit never happened. In that case, there’s no need to read the rest of this.)
When you kick of the rebase (by saving and exiting whatever editor you’re using), you’ll get a prompt that reads something like the below:
So do exactly that! Make the changes you need to make to the commit (ie delete the non-free image, put the free image back), then run
git commit --amend. That’ll replace your bad commit with the new one.(Alternative: run
git reset HEAD^at this point. That’ll make it as if the commit didn’t happen, but all the changes from the commit will be in your working copy, so you can stage/back out/whatever changes as you need. Then rungit commitas usual.)Then run
git rebase --continue, and Git will apply your second commit on top of your first one, and your history will only contain the correct fix, not the duff one.