CodeBlanks Logo Icon CodeBlanks

Reverting a specific commit

Learn to revert a specific commit.

Reverting a specific past commit

git revert can undo any commit from the past, not just the latest one (HEAD). Even if intermediate commits were made, Git isolates the changes introduced by the target commit and applies their opposite. You supply the commit reference or hash as a parameter to the command.

# Undo a specific commit from the past:
git revert --no-edit 7b2a9c1
git revert --no-edit HEAD~1

This selective approach removes an old mistake without sacrificing any useful work in between.

Conflicts during revert

If lines altered by the targeted commit were modified again in subsequent commits, a conflict may arise. Once you resolve the conflict and stage files with git add, complete the process with git revert --continue. If you wish to give up, restore the repository using git revert --abort.

# Resolve and continue:
git add file.txt
git revert --continue
# Cancel the operation entirely:
git revert --abort

This flexibility ensures complex undo operations can be handled safely.

Practice Revert a commit from history

Revert the HEAD~1 commit with git revert --no-edit -q HEAD~1 and print the latest commit subject with git log -1 --format="%s".

Starter code

terminal
git init -q -b main
git config user.name "Bahar"
git config user.email "bahar@example.com"
echo "a" > a.txt
git add a.txt
git commit -qm "A eklendi"
echo "b" > b.txt
git add b.txt
git commit -qm "B eklendi"
echo "c" > c.txt
git add c.txt
git commit -qm "C eklendi"

# HEAD~1 commit'ini revert et ve son basligi yazdir

Sample solution

terminal
git init -q -b main
git config user.name "Bahar"
git config user.email "bahar@example.com"
echo "a" > a.txt
git add a.txt
git commit -qm "A eklendi"
echo "b" > b.txt
git add b.txt
git commit -qm "B eklendi"
echo "c" > c.txt
git add c.txt
git commit -qm "C eklendi"

git revert --no-edit -q HEAD~1
git log -1 --format="%s"

Expected output

Revert "B eklendi"

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What do you do when you want to undo a specific commit after several other commits have been made?

  • Pass the commit's hash as a parameter to the git revert command Correct answer
  • Manually delete the past commit file from the .git folder
  • Delete the past commit from the stack with git stash drop
  • Reset the repo to that commit with git reset --hard, deleting all work in between

Explanation: git revert <hash> inverts only the changes of the specified commit without affecting intermediate commits.

2 Abort revert operation

Question
git revert ____
Answer
git revert --abort

Explanation: The --abort option cancels the ongoing revert process and restores the repo to its state prior to revert.

3 Commit revert commands

Term Meaning
git revert <hash> Inverts the specified commit
--continue Completes revert after conflict fix
--abort Aborts the revert operation
Seçici geri alma Leaves intermediate commits intact

All lessons in Intermediate

20 Intermediate final

  1. Branch management
  2. Rebase
  3. Conflict
  4. PR flow