Versioncontrol-Git¶
Tips - Version Control - Git¶
Table of Contents¶
Remove Commit from a Repo¶
Our case is that, we want to remove an old commit from the repo, but to keep the main commit persists.
Get the git log using:
git log --oneline # Output Example: abc1234 (HEAD -> main) Your latest commit def5678 Commit you want to delete
Use an interactive rebase to edit history. You want to rebase to two commits back (to edit the one before the latest):
git rebase -i HEAD~2
In the interactive editor, it should be something like:
pick def5678 Commit to delete pick abc1234 Your latest commit
Change it to:
drop def5678 Commit to delete pick abc1234 Your latest commit
Force-push the new history to GitHub
git push origin main --force
Optional Backup Before Starting:
git checkout -b backup-before-rebase git push origin backup-before-rebase