The git revert command allows you to safely undo changes introduced by previous commits without rewriting the commit history. Instead of deleting commits, Git creates a new commit that reverses the earlier changes, making it safe for collaborative repositories.
Git Revert Command - Quick Cheat Sheet
The following quick reference table covers the most common git revert commands and scenarios you may encounter during daily Git workflows.
| Command(s) | Description |
|---|---|
git revert <commit-hash> | Reverts a specific commit by creating a new commit that undoes the changes introduced in that commit. |
git revert HEAD | Reverts the latest commit in the current branch. |
git revert HEAD~1 | Reverts the second most recent commit. |
git revert HEAD~N | Reverts a commit N steps before the current HEAD. |
git revert <commit1> <commit2> | Reverts multiple specific commits in sequence. |
git revert <old-commit>..<new-commit> | Reverts a range of commits between two commit hashes. |
git revert HEAD~3..HEAD | Reverts the last three commits in the repository history. |
git revert -m 1 <merge-commit> | Reverts a merge commit while keeping the first parent branch. |
git revert --no-edit <commit> | Reverts a commit without opening the commit message editor. |
git revert --no-commit <commit> | Applies the revert changes but does not immediately create a new commit. |
git revert -n <commit> | Applies revert changes to the working directory so multiple reverts can be committed together. |
git revert --abort | Cancels the current revert operation if conflicts occur. |
git revert --continue | Continues the revert process after resolving conflicts. |
git revert --quit | Stops the revert sequence without creating a commit. |
git revert <commit> --strategy-option ours | Reverts a commit while preferring the current branch during conflicts. |
git revert <commit> --strategy-option theirs | Reverts a commit while preferring incoming changes during conflicts. |
git revert <commit> --signoff | Adds a Signed-off-by line to the revert commit message. |
git revert <commit> -S | Creates a GPG-signed revert commit. |
git revert <revert-commit> | Reverts a previous revert commit to restore the original changes. |
Git Revert Command Explained
What git revert does internally
The git revert command undoes the changes introduced by a specific commit by creating a new commit that applies the opposite changes. Instead of removing the original commit from the history, Git analyzes the selected commit and generates a reverse patch that cancels the modifications.
For example, if a commit added or modified files, the revert commit will remove or restore those files to their previous state. This new commit is then added on top of the current branch.
Because Git creates a new commit rather than modifying existing history, the original commit remains visible in the repository.
How git revert differs from deleting commits
A common misconception is that git revert deletes commits. In reality, it does not remove commits from the repository history.
Instead, Git performs the following actions:
- Identifies the commit to revert.
- Calculates the changes introduced by that commit.
- Creates a new commit that reverses those changes.
Example commit history before revert:
A → B → CIf commit C introduced a bug, running:
git revert Ccreates a new commit D that reverses the changes from commit C:
A → B → C → DCommit C still exists, but commit D cancels its effect.
Why git revert preserves commit history
git revert preserves history because it adds new commits instead of modifying existing ones. This behavior is important in collaborative environments where multiple developers work on the same branch.
Benefits of preserving commit history include:
- Avoiding conflicts with other developers' work
- Maintaining a complete audit trail of changes
- Making it easier to understand when and why a change was reverted
Because of this, git revert is the recommended method for undoing commits that have already been pushed to a shared repository.
Git Revert Syntax and Options
Basic git revert syntax
The general syntax of the git revert command is:
git revert [options] <commit>Example reverting a specific commit:
git revert a1b2c3This command creates a new commit that reverses the changes introduced in commit a1b2c3.
You can also revert the most recent commit using:
git revert HEADThis creates a new commit that cancels the latest commit in the current branch.
Important git revert flags explained
The git revert command supports several useful options that control how the revert operation behaves.
| Option | Description |
|---|---|
--no-edit | Reverts the commit without opening the commit message editor. |
--no-commit | Applies revert changes but does not automatically create a commit. |
-n | Same as --no-commit, allowing multiple revert changes before committing. |
-m <parent> | Used when reverting merge commits to specify which parent branch to keep. |
--signoff | Adds a Signed-off-by line to the commit message. |
-S | Creates a GPG-signed revert commit. |
--abort | Cancels the revert operation if conflicts occur. |
--continue | Continues the revert process after conflicts are resolved. |
Example reverting a commit without opening the editor:
git revert --no-edit <commit-hash>Understanding the commit hash in git revert
Every commit in Git has a unique identifier called a commit hash. This hash is used by Git to identify the exact commit you want to revert.
You can find commit hashes using:
git log --onelineExample output:
c4f8a7d Fix login bug
a8c21f1 Update README
e12a9f3 Initial commitTo revert commit c4f8a7d, run:
git revert c4f8a7dGit will generate a new commit that undoes the changes introduced in that commit.
Git Revert Workflow Explained
How git revert creates a new commit
When you run git revert, Git does not remove the original commit. Instead, it creates a new commit that contains the inverse changes of the selected commit.
For example, if the original commit added a file, the revert commit will remove that file.
Example history before revert:
A → B → CRunning:
git revert Ccreates:
A → B → C → DCommit D contains the reverse changes of commit C.
What happens to HEAD after revert
After running a revert operation, Git creates a new commit and moves the HEAD pointer to that new commit.
Example:
Before revert:
A → B → C (HEAD)After running:
git revert CHistory becomes:
A → B → C → D (HEAD)The HEAD now points to the new revert commit D.
This behavior ensures that the branch history remains intact while the unwanted changes are safely undone.
Visual explanation of git revert history
The revert workflow can be visualized as follows.
Original history:
A → B → CCommit C introduces an issue.
Running:
git revert Ccreates a new commit:
A → B → C → DCommit D cancels the changes made in commit C, restoring the repository to the state it had after commit B, while still keeping the full history intact.
Revert the Latest Commit
Sometimes the most recent commit introduces a bug or incorrect change. In such cases, you can revert the latest commit safely using git revert. This creates a new commit that reverses the changes while preserving the repository history.
Revert the most recent commit using HEAD
To revert the latest commit in the current branch, run:
git revert HEADGit will open the default commit message editor with a message similar to:
Revert "previous commit message"Once saved, Git creates a new commit that reverses the changes introduced by the latest commit.
Example commit history before revert:
A → B → C (HEAD)After running git revert HEAD:
A → B → C → D (HEAD)Commit D reverses the changes introduced by commit C.
Verify revert using git log
You can verify the revert operation by checking the commit history:
git log --onelineExample output:
d3a4c2e Revert "Add login validation"
c2f91ab Add login validation
a1b3f11 Initial commitThe new revert commit appears at the top of the history.
Understanding the generated revert commit
The revert commit contains the inverse changes of the original commit. If the original commit added a file or modified code, the revert commit will undo those changes.
This approach ensures that:
- the original commit remains in history
- the repository state is restored
- collaboration remains safe for shared branches
Revert a Specific Commit
Sometimes you need to revert a particular commit rather than the latest one. Git allows you to revert any commit using its commit hash.
Find commit hash using git log
To identify the commit you want to revert, run:
git log --onelineExample output:
c4f8a7d Fix configuration bug
a8c21f1 Add new API endpoint
e12a9f3 Initial commitEach entry contains a commit hash that uniquely identifies the commit.
Revert a commit using commit ID
To revert a specific commit, run:
git revert c4f8a7dGit will generate a new commit that reverses the changes introduced in that commit.
Example history before revert:
A → B → C → DReverting commit B produces:
A → B → C → D → ECommit E reverses the changes introduced in commit B.
Verify repository state after revert
After reverting, you can verify the changes using:
git log --onelineYou may also inspect file changes using:
git status
git diffThis confirms that the repository has returned to the expected state.
Revert Multiple Commits
In some cases, multiple commits may introduce incorrect changes. Git allows reverting several commits in one command.
Revert several commits using one command
To revert multiple commits at once, specify the commit hashes:
git revert <commit1> <commit2> <commit3>Example:
git revert a1b2c3 d4e5f6 g7h8i9Git will apply the revert operation sequentially for each commit.
How git creates multiple revert commits
Unlike some commands that combine changes, git revert creates a separate revert commit for each reverted commit.
Example history before revert:
A → B → C → DIf commits C and D are reverted, the history becomes:
A → B → C → D → E → FWhere:
- E reverts commit D
- F reverts commit C
Verify multiple reverts in commit history
You can confirm the revert operations using:
git log --onelineExample output:
f7e21c3 Revert "Add feature X"
e3a91ab Revert "Update config file"
d4e5f6 Update config file
c3d2a1b Add feature XEach reverted commit appears with a corresponding revert commit.
Revert a Range of Commits
Git also allows reverting multiple commits within a specific range.
Revert commits between two commit IDs
To revert commits between two commit hashes, use:
git revert <older-commit>..<newer-commit>Example:
git revert a1b2c3..d4e5f6This command reverts all commits that occurred between the specified commits.
Revert commits using HEAD ranges
You can also use relative references such as HEAD to revert recent commits.
Example reverting the last three commits:
git revert HEAD~3..HEADThis instructs Git to revert commits starting three commits before the current HEAD.
Example reverting last three commits
Example commit history before revert:
A → B → C → D → E (HEAD)Running:
git revert HEAD~3..HEADcreates:
A → B → C → D → E → F → G → HWhere:
- F reverts commit E
- G reverts commit D
- H reverts commit C
Each revert commit reverses the changes introduced in the original commits while preserving the complete commit history.
Revert a Merge Commit
Merge commits behave differently from normal commits because they contain changes from multiple parent branches. When reverting a merge commit, Git needs to know which parent branch should be considered the mainline.
Why reverting merge commits is different
A normal commit has one parent, but a merge commit has two parent commits. When you revert a merge commit, Git must determine which branch's changes should remain.
Example history:
A → B → C
↘
D → EIf commit E merges a feature branch into main, reverting it requires specifying the parent branch that should remain.
Without specifying the parent, Git cannot determine which changes to keep.
Using git revert -m option
To revert a merge commit, use the -m option:
git revert -m 1 <merge-commit-hash>Example:
git revert -m 1 a1b2c3The -m flag tells Git which parent branch should be treated as the mainline when generating the revert commit.
Choosing the correct parent branch
Merge commits usually have two parents:
| Parent | Meaning |
|---|---|
-m 1 | Keep the first parent (usually the main branch) |
-m 2 | Keep the merged branch changes |
In most cases, you will use:
git revert -m 1 <merge-commit>This keeps the main branch history while undoing the merged changes.
Revert a Commit That Was Already Pushed
When a commit has already been pushed to a remote repository, rewriting history using git reset can cause problems for other developers. In such cases, git revert is the safest option.
Why git revert is safe for shared repositories
The git revert command does not modify existing history. Instead, it creates a new commit that reverses previous changes.
Example history before revert:
A → B → CRunning:
git revert CCreates:
A → B → C → DCommit D cancels the changes introduced in commit C, while the history remains intact.
Because of this behavior, git revert is considered safe for shared branches and collaborative projects.
Reverting pushed commits without rewriting history
To revert a pushed commit, run:
git revert <commit-hash>Example:
git revert HEADThis creates a new commit that reverses the most recent change.
Unlike git reset, this approach does not require force pushing.
Push revert commit to remote repository
After reverting the commit locally, push the new revert commit to the remote repository:
git push origin <branch-name>Example:
git push origin mainOther developers will then receive the revert commit when pulling changes.
Revert a Revert Commit
Sometimes a commit is reverted accidentally. In such cases, you can revert the revert commit to restore the original changes.
What happens when you revert a revert
When you revert a revert commit, Git applies the reverse of the revert changes. This effectively restores the original commit.
Example history:
A → B → C → DWhere:
- C = original change
- D = revert commit
Reverting commit D restores commit C.
How to restore a reverted change
First identify the revert commit using:
git log --onelineThen revert that commit:
git revert <revert-commit-hash>This creates a new commit that re-applies the original changes.
Practical example of revert of revert
Example history:
A → B → C → DWhere commit D reverted C.
Running:
git revert DCreates:
A → B → C → D → ECommit E restores the changes originally introduced in commit C.
Revert Changes Without Creating a Commit
In some situations you may want to review or combine revert changes before committing them. Git allows this using the --no-commit option.
Using git revert --no-commit
To apply revert changes without automatically creating a commit, run:
git revert --no-commit <commit-hash>Example:
git revert --no-commit a1b2c3This applies the reversed changes to the working directory but does not create a commit yet.
Modify revert changes before committing
Once the revert changes are applied, you can review or modify them.
Check the working directory:
git statusView the changes:
git diffYou can edit files before committing the revert.
Combine multiple reverts into one commit
You can apply multiple revert operations using --no-commit and then create a single commit afterward.
Example:
git revert --no-commit commit1
git revert --no-commit commit2
git revert --no-commit commit3Then commit all changes together:
git commit -m "Revert multiple commits"This approach keeps the commit history cleaner.
Revert Only Specific Files from a Commit
Sometimes you may want to revert changes affecting only certain files rather than the entire commit.
Revert changes affecting a single file
To revert a specific file from a previous commit, restore the file version from that commit.
git checkout <commit-hash> -- <file>Example:
git checkout a1b2c3 -- config.yamlThis restores the version of the file from the selected commit.
Stage only selected revert changes
After restoring the file, stage the changes:
git add <file>Example:
git add config.yamlThis allows you to revert only specific files instead of the entire commit.
Commit partial revert safely
Finally, commit the restored changes:
git commit -m "Revert changes to config.yaml from previous commit"This creates a commit that restores the selected file while leaving other changes untouched.
Common Mistakes When Using Git Revert
While git revert is a safe command for undoing changes, developers sometimes make mistakes that lead to confusion or unexpected results. Understanding these common issues can help you avoid incorrect reversions and maintain a clean commit history.
Reverting the wrong commit
One of the most common mistakes is reverting the wrong commit. Since Git uses commit hashes to identify commits, selecting the wrong hash can revert unrelated changes.
To avoid this problem, always inspect the commit history before running the revert command.
git log --onelineExample output:
c4f8a7d Fix configuration bug
a8c21f1 Update deployment script
e12a9f3 Initial commitBefore reverting a commit, confirm that the commit hash corresponds to the change you want to undo.
You can also review the commit details:
git show <commit-hash>This ensures that you revert the correct change.
Confusion between revert and reset
Another common mistake is confusing git revert with git reset.
These commands behave very differently:
| Command | Behavior | Impact on History |
|---|---|---|
git revert | Creates a new commit that reverses previous changes | Preserves history |
git reset | Moves the branch pointer to an earlier commit | Rewrites history |
Example using revert:
git revert HEADThis safely undoes the latest commit while keeping the commit history intact.
Example using reset:
git reset --hard HEAD~1This removes the latest commit entirely and rewrites the branch history.
For shared repositories, using git revert is usually the safer option.
Reverting merge commits incorrectly
Reverting merge commits incorrectly is another common issue. Merge commits have multiple parents, and Git needs to know which parent branch should be treated as the mainline.
Attempting to revert a merge commit without specifying a parent may result in errors.
Incorrect command:
git revert <merge-commit>Correct command:
git revert -m 1 <merge-commit>The -m option specifies which parent branch should remain after the revert.
Choosing the wrong parent may produce unexpected repository changes, so it is important to verify the merge structure before reverting.
Frequently Asked Questions
1. What does git revert do?
The git revert command creates a new commit that reverses the changes introduced by a previous commit while preserving the repository history.2. How do I revert the latest commit in Git?
You can revert the most recent commit using the commandgit revert HEAD, which creates a new commit that cancels the changes from the latest commit.3. Can git revert undo multiple commits?
Yes. You can revert multiple commits usinggit revert <commit1> <commit2> or revert a range of commits using git revert <old-commit>..<new-commit>.4. How do you revert a merge commit in Git?
To revert a merge commit, usegit revert -m 1 <merge-commit-hash> and specify the parent branch that should remain.5. What happens if you revert a revert commit?
Reverting a revert commit restores the original changes because Git applies the reverse of the revert operation.Summary
The git revert command provides a safe and reliable way to undo changes introduced by previous commits while preserving the complete commit history. Instead of deleting commits, Git creates a new commit that reverses earlier modifications, making it ideal for collaborative projects and shared repositories.
In this guide, we explored how git revert works internally, examined its syntax and options, and demonstrated practical scenarios such as reverting single commits, multiple commits, ranges of commits, merge commits, and pushed commits. We also covered advanced use cases like reverting a revert commit, applying reverts without committing immediately, and restoring specific file changes.
Understanding these workflows helps developers safely undo problematic changes without disrupting the repository history.
Official Documentation
For additional details about the git revert command and related Git functionality, refer to the official Git documentation:



![Git Error: Cannot Delete Branch Checked Out or Used by Worktree [SOLVED]](/cannot-delete-branch-checked-out-at/git-cannot-delete-branch_hu_3f86bd25a59d627d.webp)






