Git Foundations / Undo Changes Safely

Revert a shared commit

Lesson 12 of 33. Open to read. No account needed.

Undoing Without Rewriting

Amending replaces a commit, which is fine while nobody else has it. Once a commit is shared, or when the bad commit is several commits back, you need a way to undo it that leaves the existing history alone.

git revert does that. It works out the opposite of a commit's changes and records them as a new commit. Lines the commit added are removed, lines it removed come back, and every other commit stays exactly as it was.

What the History Looks Like

Two histories side by side, newest commit at the top. Before: Use dark theme, Add contact page, Raise timeout to 300 (the bad change) and Add app settings. After git revert: the same four commits, still in history, with a new commit at the top that undoes the timeout change.

In the diagram, "Raise timeout to 300" was a mistake, but two good commits came after it. After the revert:

  • The bad commit is still in the history. Anyone reading the log can see what happened and when it was undone.
  • The later commits, "Add contact page" and "Use dark theme", are untouched, and so are their changes.
  • A new commit at the top holds the opposite change, setting the timeout back.

Because nothing is removed or replaced, a revert is safe for commits other people already have. It is just one more commit for them to receive.

Running a Revert

Name the commit to undo, by ID or by a name such as HEAD~2:

git revert --no-edit <commit>

Git writes a message for the new commit: Revert "<original summary>", with a body saying This reverts commit <full ID>. Without --no-edit, Git opens your editor with that message so you can add the reason for the revert; save and close the editor to finish.

Start with a clean git status. If you have uncommitted edits to a file the revert needs to change, Git stops with Your local changes to the following files would be overwritten and fatal: revert failed, and changes nothing.

When a Revert Conflicts

A revert can only undo lines that still look the way the commit left them. If a later commit changed the same lines again, Git cannot tell what the result should be, so it stops with a conflict and asks you to decide.

In a conflicted file, Git marks the disputed lines:

<<<<<<< HEAD
retries = 5  # attempts before giving up
=======
>>>>>>> parent of 6569da9 (Add retry limit)
  • Between <<<<<<< HEAD and ======= is what the file contains now.
  • Between ======= and >>>>>>> is what the revert wants, here nothing, because reverting the commit that added the line means removing it.

To resolve it, edit the file so it reads the way it should, with all three marker lines gone, then mark it resolved and finish:

git add <file>
git revert --continue

git revert --continue opens your editor with the prepared message; save and close it. If you would rather not continue, git revert --abort puts everything back as it was before the revert started. The branching module covers conflicts in much more depth.

Exercise: Undo a Commit in the Middle of a History

A settings file had its timeout raised by mistake, and two good commits came after. You will revert the bad commit, then revert a second one that conflicts.

Starting state: Git configured as in the configuration lesson.

  1. Run the setup. It makes four commits:

    mkdir -p ~/git-practice/revert-practice
    cd ~/git-practice/revert-practice
    git init -b main
    cat > config.txt <<'EOF'
    # App settings
    theme = light
    language = en
    timeout = 30
    EOF
    git add config.txt
    git commit -m "Add app settings"
    cat > config.txt <<'EOF'
    # App settings
    theme = light
    language = en
    timeout = 300
    EOF
    git commit -am "Raise timeout to 300"
    cat > contact.md <<'EOF'
    # Contact
    
    Email the team with questions.
    EOF
    git add contact.md
    git commit -m "Add contact page"
    cat > config.txt <<'EOF'
    # App settings
    theme = dark
    language = en
    timeout = 300
    EOF
    git commit -am "Use dark theme"
    
  2. Find the bad commit and confirm what it changed:

    git log --oneline
    git show HEAD~2
    

    "Raise timeout to 300" is two steps back from HEAD, and its changes show -timeout = 30 and +timeout = 300.

  3. Revert it:

    git revert --no-edit HEAD~2
    
  4. Check the result:

    git log --oneline
    cat config.txt
    ls
    git show
    

    The log has five commits, with Revert "Raise timeout to 300" on top and the bad commit still below. config.txt has timeout = 30 and still has theme = dark from the later commit. contact.md is still there. git show displays the opposite change, -timeout = 300 and +timeout = 30, under the message This reverts commit followed by the bad commit's full ID.

  5. Add a setting, then change the same line again in a second commit:

    cat >> config.txt <<'EOF'
    retries = 5
    EOF
    git commit -am "Add retry limit"
    cat > config.txt <<'EOF'
    # App settings
    theme = dark
    language = en
    timeout = 30
    retries = 5  # attempts before giving up
    EOF
    git commit -am "Explain the retry limit"
    
  6. The team decides there should be no retry limit. Revert the commit that added it, which is one step back:

    git revert --no-edit HEAD~1
    

    Git reports CONFLICT (content): Merge conflict in config.txt and error: could not revert. Look at the state:

    git status
    cat config.txt
    

    git status says You are currently reverting commit and lists both modified: config.txt. The file ends with the conflict markers shown in the When a Revert Conflicts section. The later commit changed the line the revert wants to remove.

  7. Decide: the setting should go, comment and all. Write the file without the retry line and without the markers, then finish the revert. Your editor opens with the message Revert "Add retry limit"; save and close it.

    cat > config.txt <<'EOF'
    # App settings
    theme = dark
    language = en
    timeout = 30
    EOF
    git add config.txt
    git revert --continue
    
  8. Check:

    git log --oneline -3
    cat config.txt
    

    The newest commit is Revert "Add retry limit", and config.txt has no retry line.

Success: both bad changes are undone by new revert commits, the later good changes are still in place, and git log still shows every original commit.

If something differs:

What you seeLikely causeNext step
An editor opened during git revertYou left out --no-editSave and close it to accept the message
The wrong commit was revertedHEAD~n counted from the wrong commitgit revert --no-edit HEAD reverts the revert and brings the change back; then revert the right commit
Your local changes to the following files would be overwrittenUncommitted edits to a file the revert changesCommit or discard them, then revert again
error: no cherry-pick or revert in progressThere was no conflict, or the revert already finishedCheck git log --oneline; the revert commit is probably there
Conflict markers left in the committed fileThe file was added before all three marker lines were removedFix the file and commit the correction
  1. Clean up:

    cd ~/git-practice
    ls
    rm -rf revert-practice
    ls
    

Independent task: before cleaning up, the contact page should not have been published. Undo the commit "Add contact page" without changing any other commit. Find its ID with git log --oneline rather than counting steps from HEAD. Then check that contact.md is gone and that "Add contact page" is still in the log.

Check Your Understanding

  1. After git revert, is the reverted commit still in git log? Why does that make revert safe for shared commits?
  2. A colleague suggests deleting the bad commit from the history instead of reverting it, although the team already has it. What problem would that cause?

Answer and Reasoning

Independent task

git log --oneline
git revert --no-edit <ID>

Use the ID shown next to "Add contact page" in your log. The revert commit removes contact.md, so ls no longer lists it, and the newest commit is Revert "Add contact page". git log --oneline still lists "Add contact page" further down: reverting never removes the original commit. No conflict occurs, because no later commit changed contact.md.

Understanding check

  1. Yes. A revert adds a new commit containing the opposite change and leaves the original where it was. Because nothing in the existing history changes, everyone who already has those commits can simply receive the new one.
  2. Deleting a commit others have means rewriting the history after it, so your history and your teammates' would no longer match, and their copies would still contain the bad commit. A revert undoes the change while keeping one shared history. The next lesson shows the commands that move a branch back and when they are appropriate.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy