Reverse a recorded change

Lesson 22 of 25 · Git: records, branches, and collaboration

When a faulty change is already shared, preserve the history others have and add a commit that reverses the change. In this example, B changes a setting and C is a later, independent commit:

A──B──C  shared branch
      └──R  after reverting B

R records the inverse of B’s change on top of C; it does not delete B or reset every file to A. Inspect the history and the target first. With a clean working tree and a suitable ordinary, single-parent commit, replace the placeholder with its actual ID:

git status
git log --oneline
git show <commit-id>
git revert <commit-id>

Git normally opens an editor for the new commit message; save and close it to finish. Review with git show HEAD and test the resulting behavior. A later change may overlap with B, so the inverse can conflict or need adjustment. If Git stops, inspect git status, edit conflicted files to the intended result, remove markers, stage each resolution with git add <path>, then run git revert --continue. To abandon an unfinished revert, git revert --abort attempts to return to the pre-revert state; start clean to avoid complicating recovery.

Reverting a merge commit requires selecting a mainline parent with -m and has consequences for later merges; examine the merge and team policy first rather than treating it like an ordinary commit. A revert does not erase a secret from history or other copies: revoke or rotate an exposed credential and follow the appropriate incident process.

For an unshared latest commit, git commit --amend can replace it using the current index and a new or edited message. It creates a different commit rather than editing the old object. Inspect staged content first and coordinate if the original was shared; amend is not a safe default for published history.