(Git) Edit/delete an in-the-middle commit from history
-
Safe approach: Revert
git revert <sha1-commit-hash>
where the hash is of the commit to revert. This creates a new commit with all the previous commit changes undone.
-
Unsafe approach: Interactive rebase
git rebase -i <sha1-commit-hash>
where the hash points to a successful commit prior to the commit to be deleted. This brings up an editor where each commit following the hash commit above is listed. Earlier commit (one closer to the hash commit) is at the top of the editor page. Change
pick
todrop
to remove (or edit) a past commit (oredit
if wishing to update a commit changes, andreword
to update a commit message).- When
edit
ing, make changes to code and stage them, then rungit rebase --continue
. - NOTE: To edit the most recent, not-yet-pushed commit, just use
git commit --amend
.
- When
Reference:
Written on February 11, 2021