Undo a shared commit with git revert

한국어로 읽기 →

Cause: Removing a shared commit changes the history that collaborators rely on.

Fix: Use revert to create a new commit that undoes the changes. Save work in progress separately, then inspect the working state and recent commits.

git status --short
git log --oneline -2

Run the next command only when the working tree is clean and HEAD, the current last commit, is the intended target.

git revert --no-edit HEAD

A conflict means the new commit is not complete. Follow the conflict-resolution path below, or choose the separate cancellation path.

Conceptual illustration of a new commit with an arrow returning toward earlier history.
Undo changes without removing history

These screenshots show the actual commands and results. Select an image to open it at full size. On small screens, scroll sideways inside a wide image.

1. Check the changes in the commit to undo

Save any work in progress first, then check the commit to revert.

git log --oneline -2
git show HEAD

Read the changed files and added or removed lines in git show HEAD before confirming the target. The capture shows that the last commit, Enable feature, changed the setting to feature=on. HEAD means the current last commit. To revert a different commit, inspect the history and changed files to select the correct commit ID. Review merge commits separately from this single-commit example, because you must decide which parent to use as the basis for the revert.

Commit to revert and enabled setting
Look for: Commit to revert and setting

Expected resultYou can identify the last commit and the enabled setting.

2. Create a new commit that undoes the changes

Once you have confirmed the target and cleared any work in progress, run this command.

git revert --no-edit HEAD

revert applies the inverse of the selected commit's changes and records a new commit. --no-edit keeps the default revert message. The capture shows a new Revert "Enable feature" commit. On success, go to the final verification step. If CONFLICT appears, the new commit is not complete; use the next section.

Resolve conflicted files, stage them, and continue

Use this section only when revert stops with a conflict. Suppose settings.txt originally contained feature=off, a commit changed it to feature=on, and a later commit changed the same line to feature=pilot. Reverting the earlier change can conflict. Read both the current content and the change being undone before deciding on the final value.

git status --short

This exercise produced UU settings.txt. Open the file and inspect the alternatives between <<<<<<<, =======, and >>>>>>>. Decide whether the feature should be disabled; do not merely remove the markers. For that goal, keep feature=off, remove every conflict marker, and save the file. Preserve unrelated settings such as timeout=30. Other repositories may show additional files or addition/deletion conflicts.

Review the saved content, then stage only the resolved files. Replace the example path with the actual conflicted path.

git diff --check

Resolve any reported errors before staging the files:

git add -- settings.txt
git diff --cached -- settings.txt

If git diff --check reports leftover conflict markers or whitespace errors, fix them first. Empty output does not prove the feature works. In this example, the staged diff should change feature=pilot to feature=off. Review and stage every conflicted file before continuing.

git revert --continue

If a message editor opens, check the target and reason, then save and close it. A completed operation creates Revert "Enable feature" while retaining the original Enable feature commit and the later commit. Continue to the final history and behavior checks.

This additional example was run on 2026/10/20 (year/month/day) in an isolated synthetic repository with Git 2.52.0.windows.1. The new commit, feature=off, and preservation of the other file were verified. The existing captures show the earlier conflict-free path.

Expected result: The conflict is resolved and a new revert commit contains the intended final content.

Cancel instead of continuing

If you chose the wrong target or cannot yet decide the final content, cancel instead of continuing. This also abandons edits made while resolving the conflict, so save a separate copy first if you need any of them.

git revert --abort
git status --short
git log --oneline -3

Cancellation returns to the state before this revert began. In a separate run of the same synthetic example, feature=pilot and the original HEAD were preserved, and no Revert commit was created. Because the working tree started clean, git status --short was also empty.

Stop this path here; do not proceed to the push step below. Reconsider the target and desired behavior before starting another revert.

Expected result: The pre-revert state is restored without a new commit, completing the cancellation path.

New commit that reverses the changes
Look for: New commit that reverses the changes

Expected resultA new commit named Revert "Enable feature" is created.

3. Check the preserved history and restored state, then push

Check that the original commit remains and the feature is back to its previous state.

git log --oneline -3

The capture shows a revert commit above Enable feature, and the file contains feature=off. Test the feature, then push after confirming the destination repository and branch.

git push origin main
git status --short --branch

After a successful push, the status shows that the branch matches the remote. If the push is rejected, check remote changes and permissions before resolving the issue.

Preserved history and successful push
Look for: Preserved history and remote update

Expected resultBoth the original and revert commits remain, and the new commit is reflected on the remote.

References

Another Git problem?

Trace file history and renames with Git log →

Compare staged changes and working files with Git diff →

Use git cherry-pick to bring in just the commit you need →