git revert
- Git course
- Intermediate
- Lesson 37/82
- 4 min
Learn to create a reverse commit with git revert.
Safe undo with git revert
The git revert command creates a brand new commit that inverts the changes introduced by an earlier commit. Instead of deleting or rewriting history, it records the opposite of the operation as a forward-moving commit. This keeps project history intact and avoids conflicts with team repositories.
# Invert the HEAD commit:
git revert HEADThis is the safest way to undo mistakes, especially on branches shared with others.
Skipping commit editor: --no-edit
Normally, when git revert runs, Git automatically prepares a commit message and opens the terminal editor. To accept Git's default revert message directly without opening an editor, pass the --no-edit flag. If you want to stage the inverted changes without committing immediately, use -n.
git revert --no-edit HEAD
git revert -n HEADThe --no-edit flag is especially handy in scripts and quick fixes.
Practice Undo latest commit with revert
Undo the latest commit using git revert --no-edit -q HEAD and print that the file returned to its initial state using cat app.py.
Starter code
git init -q -b main
git config user.name "Bora"
git config user.email "bora@example.com"
echo "v1" > app.py
git add app.py
git commit -qm "v1 hazir"
echo "hatali kod" >> app.py
git add app.py
git commit -qm "Hatali ekleme"
# Son commit'i revert et ve app.py icerigini basSample solution
git init -q -b main
git config user.name "Bora"
git config user.email "bora@example.com"
echo "v1" > app.py
git add app.py
git commit -qm "v1 hazir"
echo "hatali kod" >> app.py
git add app.py
git commit -qm "Hatali ekleme"
git revert --no-edit -q HEAD
cat app.pyExpected output
v1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does Git do when the git revert HEAD command is executed?
- Permanently deletes and removes the HEAD commit from the repo
- Creates a new commit that reverses the changes introduced by HEAD Correct answer
- Moves all files in the working directory to the staging area
- Updates only the author name and date of the latest commit
Explanation: git revert keeps history safe by producing a new commit containing the inverse of the target commit's changes.
2 Accept default revert message directly
git revert ____ HEAD
git revert --no-edit HEAD
Explanation: The --no-edit flag directly commits Git's default revert message without opening a text editor.
3 git revert fundamentals
| Term | Meaning |
|---|---|
git revert |
Creates a new inverse commit |
Ters commit |
Removes added lines, adds deleted ones |
--no-edit |
Directly accepts the default message |
-n |
Reverts but does not commit |