ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example |
| Last Crawled | 2026-04-15 14:25:21 (2 days ago) |
| First Indexed | 2018-07-24 04:40:13 (7 years ago) |
| HTTP Status Code | 200 |
| Meta Title | How to revert a Git commit: A simple example | TheServerSide |
| Meta Description | In this quick git revert example, we'll show you how to revert a Git commit and undo unwanted changes. |
| Meta Canonical | null |
| Boilerpipe Text | The most misunderstood operation in the world of distributed version control must be the
git revert
command. Let's walk through an example of how to revert a Git commit, and differentiate the
git reset
and
git revert
commands.
The purpose of the
git revert
command is to remove all the changes a single commit made to your source code repository. For example, if a past commit added a file named
index.html
to the repo, a git revert on that commit will remove the
index.html
file from the repo. If a past commit added a new line of code to a Java file, a
git revert
on that commit will remove the added line.
When you revert a Git commit, the changes from the targeted commit are removed from your local workspace. A new commit is also created to reflect the new state of your repository.
The git revert command
The syntax to revert a Git commit and undo unwanted changes is simple. All developers need to do is issue the
git revert
command and provide the ID of the commit to undo:
git@commit
/c/revert example/
$ git revert 4945db2
A git revert commit example
To really understand how to
undo Git commits
, look at this
git revert
example.
We will start with a
git init
command to create a completely clean repository:
git@commit
/c/revert example/
$ git init
Initialized empty Git repo in C:/git revert example
With the repository initialized, we'll add five files to the repo. Each time a new file is created, we add it to the Git index and create a new commit with a meaningful message.
git@commit
/c/revert example/
$ touch alpha.html
$ git add . && git commit -m "1st git commit: 1 file"
$ touch beta.html
$ git add . && git commit -m "2nd git commit: 2 files"
$ touch charlie.html
$ git add . && git commit -m "3rd git commit: 3 files"
$ touch delta.html
$ git add . && git commit -m "4th git commit: 4 files"
$ touch edison.html
$ git add . && git commit -m "5th git commit: 5 files"
A quick directory listing following the initial command batch shows five files in the current folder:
git@commit
/c/revert example/
$ ls
alpha.html beta.html charlie.html delta.html edison.html
A call to the
git reflog
command
will show us our current commit history:
git@commit
/c/revert example/
$ git reflog
(HEAD -> master)
d846aa8 HEAD@{0}: commit: 5th git commit: 5 files
0c59891 HEAD@{1}: commit: 4th git commit: 4 files
4945db2 HEAD@{2}: commit: 3rd git commit: 3 files
defc4eb HEAD@{3}: commit: 2nd git commit: 2 files
2938ee3 HEAD@{4}: commit: 1st git commit: 1 file
How to revert a Git commit
What do you think would happen if we did a
git revert
on the third commit with ID 4945db2? This was the git commit that added the
charlie.html
file.
git@commit
/c/revert example/
$ git revert 4945db2
Will the
git revert
of commit 4945db2 remove
charlie.html
,
delta.html
and
edison.html
from the local workspace?
Will the
git revert
of commit 4945db2 remove
delta.html
and
edison.html
from the local workspace, but leave the other files alone?
Or will this
git revert
example leave four files in the local workspace and remove only the
charlie.html
file?
If you chose the last outcome, you'd be correct. Here's why.
The
git revert
command will undo only the changes associated with a specific commit. In this
git revert
example, the third commit added the
charlie.html
file. When we revert said Git commit, the only file removed from our repository is
charlie.html
.
git@commit
/c/revert example/
$ ls
alpha.html beta.html delta.html edison.html
Developers also need to know that when they
git revert
a commit, the reverted commit is deleted from their local workspace, but not deleted from the local repository. The code associated with the reverted Git commit remains stored in the repository's history of changes, which means reverted code is still referenceable if it ever needs to be accessed or reviewed in the future.
Options like continue, abort and edit help to simplify the process of how to git revert a commit.
Compare git revert vs. reset
When you
git revert
a commit, only the changes associated with that commit are undone. Cumulative changes from subsequent commits aren't affected. If you wish to undo every change since a given commit occurred, you'd want to issue a
hard
git reset
, not
revert
.
The
git reset
and
revert
commands are commonly confused, but they apply to distinct use cases. To undo changes associated with a specific commit, developers should use the
git revert
command. To
undo every change
that has happened since a given commit occurred, use
git reset
. In the specific case where a developer needs to undo only the previous commit, either of the
git revert
or
git reset
commands will suffice.
Steps to revert a Git commit
In review, the steps to
git revert
a commit and undo unwanted changes are the following:
Locate the ID of the commit to revert with the git log or reflog command.
Issue the
git revert
command and provide the commit ID of interest.
Supply a meaningful
Git commit message
to describe why the revert was needed.
The
git revert
command is a simple way to remove a bug introduced to the version control system at some point in the past, or back out of a feature enhancement that wasn't well-received by the client. If you want to undo changes that happened in a specific commit, the
git revert
command is the correct operation to choose.
Next Steps
Git fork vs. clone: What's the difference?
Uncommit Git's last commit
Git vs. GitHub: What is the difference between them?
Dig Deeper on DevOps-driven, cloud-native app development
How to unstage a file in Git
By:
Cameron McKenzie
How to publish GitHub Actions artifacts by example
By:
Cameron McKenzie
How to git cherry-pick a commit example
By:
Cameron McKenzie
Git reset hard vs. soft: What's the difference?
By:
Cameron McKenzie |
| Markdown | [](https://www.theserverside.com/) [ ](https://www.theserverside.com/) [](https://www.techtarget.com/)
[Sign-up now. Start my free, unlimited access.](https://www.theserverside.com/login?fromURL=https%3A%2F%2Fwww.theserverside.com%2Ftutorial%2FHow-to-git-revert-a-commit-A-simple-undo-changes-example)
[Login](https://www.theserverside.com/login?fromURL=https%3A%2F%2Fwww.theserverside.com%2Ftutorial%2FHow-to-git-revert-a-commit-A-simple-undo-changes-example) [Register](https://www.theserverside.com/register?fromURL=https%3A%2F%2Fwww.theserverside.com%2Ftutorial%2FHow-to-git-revert-a-commit-A-simple-undo-changes-example)
[Informa TechTarget](https://www.informatechtarget.com/) [Explore the Network](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example)
- [App Architecture](https://www.techtarget.com/searchapparchitecture/)
- [Software Quality](https://www.techtarget.com/searchsoftwarequality/)
- [Cloud Computing](https://www.techtarget.com/searchcloudcomputing/)
- [Security](https://www.techtarget.com/searchsecurity/)
- [AWS](https://www.techtarget.com/searchaws/)
[An Informa TechTarget Publication](https://www.informatechtarget.com/) [Explore our brands](https://www.informatechtarget.com/our-brands)
[RSS](https://www.theserverside.com/rss)
- - [Core Java](https://www.theserverside.com/resources/Core-Java-APIs-and-programming-techniques)
- [DevOps](https://www.theserverside.com/resources/DevOps-driven-cloud-native-app-development)
- [More Topics](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example)
- [Development](https://www.theserverside.com/resources/Software-development-best-practices-and-processes)
- [Frameworks](https://www.theserverside.com/resources/Front-end-back-end-and-middle-tier-frameworks)
- [Skills and career](https://www.theserverside.com/resources/Professional-skills-development-and-career-advice)
- [Tools](https://www.theserverside.com/resources/Development-tools-for-continuous-software-delivery)
[Other Content](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example)
- [Videos](https://www.theserverside.com/videos)
- [Definitions](https://www.theserverside.com/definitions)
- [Webinars](https://www.theserverside.com/webinars)
- [Sponsored Sites](https://www.theserverside.com/sponsored_communities)
- [More](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example)
- [Answers](https://www.theserverside.com/answers)
- [Features](https://www.theserverside.com/features)
- [Opinions](https://www.theserverside.com/opinions)
- [Quizzes](https://www.theserverside.com/quizzes)
- [Tech Accelerators](https://www.theserverside.com/techaccelerators)
- [Tips](https://www.theserverside.com/tips)
- [Tutorials](https://www.theserverside.com/tutorials)
### [Essential Guide](https://www.theserverside.com/guides)
Browse Sections
- Getting started with Git
- [IT pros weigh Git version control against the competition](https://www.techtarget.com/searchsoftwarequality/feature/IT-pros-weigh-Git-version-control-against-the-competition)
- [Step-by-step guide: How to install Git on Windows desktop computers](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Step-by-step-guide-to-install-Git-on-Windows-desktop-computers)
- [5 basic Git commands developers must master: Tutorial with examples](https://www.theserverside.com/tutorial/Five-basic-Git-commands-every-beginner-needs-to-know)
- [How not to write a Git commit message](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-not-to-write-a-Git-commit-message)
- [How to set Notepad++ as the default Git editor for commits instead of Vim](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-to-set-Notepad-as-the-default-Git-editor-for-commits-instead-of-Vim)
- Git and Github
- [Git vs. GitHub: What's the difference?](https://www.theserverside.com/video/Git-vs-GitHub-What-is-the-difference-between-them)
- [Microsoft-GitHub acquisition shakes up DevOps market](https://www.techtarget.com/searchitoperations/news/252442473/Microsoft-GitHub-acquisition-shakes-up-DevOps-market)
- [Want a private GitHub repository? It comes with a catch](https://www.theserverside.com/feature/Want-a-private-GitHub-repository-It-comes-with-a-catch)
- [How to Git started and use the Github Desktop app tool](https://www.theserverside.com/video/Test-Git-command-basics-on-the-GitHub-Desktop-app)
- [Tough sample GitHub interview questions and answers for job candidates](https://www.theserverside.com/video/Tough-sample-Git-and-GitHub-interview-questions-and-answers)
- Fix bad mistakes with Git
- [How to revert a Git commit: A simple undo changes example](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example)
- [A Git reset hard example: An easy way to undo local commits and shift head](https://www.theserverside.com/video/How-to-use-the-git-reset-hard-command-to-change-a-commit-history)
- [Don't Git revert that last commit, Git reset instead](https://www.theserverside.com/video/Dont-git-revert-that-last-commit-git-reset-instead)
- [Learn to Git cherry-pick a commit with this easy example](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Need-to-git-cherry-pick-a-commit-Heres-an-example-how)
- [How to 'Git cherry-pick' from another branch to your own](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-to-git-cherry-pick-from-another-branch-to-your-own)
- Git integration with Jenkins
- [Jenkins Git integration: GitHub pull request via the Git plugin](https://www.theserverside.com/tutorial/Attain-Jenkins-Git-integration-with-a-GitHub-pull-request)
- [How to use the Jenkins Git Plugin: Tips and tricks](https://www.theserverside.com/video/Tips-and-tricks-on-how-to-use-Jenkins-Git-Plugin)
- [Jenkins Git environment variables list: A working shell script example](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Complete-Jenkins-Git-environment-variables-list-for-batch-jobs-and-shell-script-builds)
- [Use the Jenkins OAuth plug-in to securely pull from GitHub](https://www.theserverside.com/tutorial/Use-the-Jenkins-OAuth-plug-in-to-securely-pull-from-GitHub)
- [Tough sample GitHub interview questions and answers for job candidates](https://www.theserverside.com/video/Tough-sample-Git-and-GitHub-interview-questions-and-answers)
- Where Git fits in your distributed version control system tool belt
- [Follow these Git commit message guidelines](https://www.theserverside.com/video/Follow-these-git-commit-message-guidelines)
- [Where system, global and local Windows Git config files are saved](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Where-system-global-and-local-Windows-Git-config-files-are-saved)
- [How Atomist's Rod Johnson works with pull requests](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-Atomists-Rod-Johnson-works-with-pull-requests)
- [Five ways to fix Git's 'fatal: repository not found' error](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Five-ways-to-fix-Gits-fatal-repository-not-found-error)
- [10 Git quiz questions to test your distributed version control skills](https://www.theserverside.com/quiz/Prove-your-DVCS-IQ-with-our-Git-quiz-for-beginners)
This content is part of the Essential Guide: [Master Git basics and branch into DVCS](https://www.theserverside.com/essentialguide/Master-Git-basics-and-branch-into-DVCS)
[Tutorial](https://www.theserverside.com/tutorials)
# How to revert a Git commit: A simple example
## The git revert command is commonly misunderstood. In this quick tutorial, we will show you exactly how the command works and how to perform a simple undo in your repo.
- Share this item with your network:

By
- [Cameron McKenzie,](https://www.techtarget.com/contributor/Cameron-McKenzie) TechTarget
Published: 01 Dec 2021
The most misunderstood operation in the world of distributed version control must be the git revert command. Let's walk through an example of how to revert a Git commit, and differentiate the git reset and git revert commands.
The purpose of the git revert command is to remove all the changes a single commit made to your source code repository. For example, if a past commit added a file named index.html to the repo, a git revert on that commit will remove the index.html file from the repo. If a past commit added a new line of code to a Java file, a git revert on that commit will remove the added line.
When you revert a Git commit, the changes from the targeted commit are removed from your local workspace. A new commit is also created to reflect the new state of your repository.
## The git revert command
The syntax to revert a Git commit and undo unwanted changes is simple. All developers need to do is issue the git revert command and provide the ID of the commit to undo:
```
git@commit /c/revert example/
$ git revert 4945db2
```
## A git revert commit example
To really understand how to [undo Git commits](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/git-unstage-file-all-index-commit-folder-add-delete), look at this git revert example.
We will start with a [git init](https://www.theserverside.com/video/How-to-create-a-local-repository-with-the-git-init-command) command to create a completely clean repository:
```
git@commit /c/revert example/
$ git init
Initialized empty Git repo in C:/git revert example
```
With the repository initialized, we'll add five files to the repo. Each time a new file is created, we add it to the Git index and create a new commit with a meaningful message.
```
git@commit /c/revert example/
$ touch alpha.html
$ git add . && git commit -m "1st git commit: 1 file"
$ touch beta.html
$ git add . && git commit -m "2nd git commit: 2 files"
$ touch charlie.html
$ git add . && git commit -m "3rd git commit: 3 files"
$ touch delta.html
$ git add . && git commit -m "4th git commit: 4 files"
$ touch edison.html
$ git add . && git commit -m "5th git commit: 5 files"
```
A quick directory listing following the initial command batch shows five files in the current folder:
```
git@commit /c/revert example/
$ ls
alpha.html beta.html charlie.html delta.html edison.html
```
A call to the [git reflog command](https://www.theserverside.com/video/Git-reflog-vs-log-How-these-commit-history-tools-differ) will show us our current commit history:
```
git@commit /c/revert example/
$ git reflog
(HEAD -> master)
d846aa8 HEAD@{0}: commit: 5th git commit: 5 files
0c59891 HEAD@{1}: commit: 4th git commit: 4 files
4945db2 HEAD@{2}: commit: 3rd git commit: 3 files
defc4eb HEAD@{3}: commit: 2nd git commit: 2 files
2938ee3 HEAD@{4}: commit: 1st git commit: 1 file
```
## How to revert a Git commit
What do you think would happen if we did a git revert on the third commit with ID 4945db2? This was the git commit that added the charlie.html file.
```
git@commit /c/revert example/
$ git revert 4945db2
```
Will the git revert of commit 4945db2 remove charlie.html, delta.html and edison.html from the local workspace?
Will the git revert of commit 4945db2 remove delta.html and edison.html from the local workspace, but leave the other files alone?
Or will this git revert example leave four files in the local workspace and remove only the charlie.html file?
If you chose the last outcome, you'd be correct. Here's why.
The git revert command will undo only the changes associated with a specific commit. In this git revert example, the third commit added the charlie.html file. When we revert said Git commit, the only file removed from our repository is charlie.html.
```
git@commit /c/revert example/
$ ls
alpha.html beta.html delta.html edison.html
```
Developers also need to know that when they git revert a commit, the reverted commit is deleted from their local workspace, but not deleted from the local repository. The code associated with the reverted Git commit remains stored in the repository's history of changes, which means reverted code is still referenceable if it ever needs to be accessed or reviewed in the future.

Options like continue, abort and edit help to simplify the process of how to git revert a commit.
## Compare git revert vs. reset
When you git revert a commit, only the changes associated with that commit are undone. Cumulative changes from subsequent commits aren't affected. If you wish to undo every change since a given commit occurred, you'd want to issue a [hard git reset](https://www.theserverside.com/video/How-to-use-the-git-reset-hard-command-to-change-a-commit-history), not revert.
The git reset and revert commands are commonly confused, but they apply to distinct use cases. To undo changes associated with a specific commit, developers should use the git revert command. To [undo every change](https://www.theserverside.com/video/Dont-git-revert-that-last-commit-git-reset-instead) that has happened since a given commit occurred, use git reset. In the specific case where a developer needs to undo only the previous commit, either of the git revert or git reset commands will suffice.
## Steps to revert a Git commit
In review, the steps to git revert a commit and undo unwanted changes are the following:
1. Locate the ID of the commit to revert with the git log or reflog command.
2. Issue the git revert command and provide the commit ID of interest.
3. Supply a meaningful [Git commit message](https://www.theserverside.com/video/Follow-these-git-commit-message-guidelines) to describe why the revert was needed.
The git revert command is a simple way to remove a bug introduced to the version control system at some point in the past, or back out of a feature enhancement that wasn't well-received by the client. If you want to undo changes that happened in a specific commit, the git revert command is the correct operation to choose.
#### Next Steps
[Git fork vs. clone: What's the difference?](https://www.theserverside.com/answer/Git-fork-vs-clone-Whats-the-difference)
[Uncommit Git's last commit](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/uncommit-git-last-commit-file-changes-pushed-deleted-message)
[Git vs. GitHub: What is the difference between them?](https://www.theserverside.com/video/Git-vs-GitHub-What-is-the-difference-between-them)
#### Dig Deeper on DevOps-driven, cloud-native app development
- [ How to unstage a file in Git  By: Cameron McKenzie](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/git-unstage-file-all-index-commit-folder-add-delete)
- [ How to publish GitHub Actions artifacts by example  By: Cameron McKenzie](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-to-publish-GitHub-Actions-artifacts-example)
- [ How to git cherry-pick a commit example  By: Cameron McKenzie](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Need-to-git-cherry-pick-a-commit-Heres-an-example-how)
- [ Git reset hard vs. soft: What's the difference?  By: Cameron McKenzie](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Git-reset-hard-vs-soft-Whats-the-difference)
Sponsored News
- [Sustainable AI in Action: 3 Real-World Examples](https://www.techtarget.com/searchenterpriseai/PrivateAI/Sustainable-AI-in-Action-3-Real-World-Examples) –Equinix
- [Keeping the Human in Healthcare: Rethinking Health IT for Real Connection](https://www.techtarget.com/revcyclemanagement/GreenwayHealth/Keeping-the-Human-in-Healthcare-Rethinking-Health-IT-for-Real-Connection) –Greenway Health
- [What To Know About Insurance Eligibility, Discovery, and Verification](https://www.techtarget.com/revcyclemanagement/ZOLLDataSystems/What-To-Know-About-Insurance-Eligibility-Discovery-and-Verification) –ZOLL Data Systems
- [See More](https://www.theserverside.com/sponsored_communities)
Latest TechTarget resources
- [Application Architecture](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example)
- [Software Quality](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example)
- [Cloud Computing](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example)
- [Security](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example)
- [AWS](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example)
[Search App Architecture](https://www.techtarget.com/searchapparchitecture/)
- [AI Agents' role in IT infrastructure is expanding](https://www.techtarget.com/searchapparchitecture/tip/AI-Agents-role-in-IT-infrastructure-is-expanding)
AI agents are moving from passive monitoring to active operators, handling incidents, optimizing infrastructure and forcing teams...
- [Pipeline as Code: Managing CI/CD complexity and sprawl](https://www.techtarget.com/searchapparchitecture/tip/Pipeline-as-Code-Managing-CI-CD-complexity-and-sprawl)
Pipeline as Code enhances CI/CD with automation and consistency but adds complexity. Explore benefits, risks and tools to improve...
- [What repos are trending on GitHub?](https://www.techtarget.com/searchapparchitecture/tip/What-repos-are-trending-on-GitHub)
GitHub Stars are a proxy for developer interest. Weekly GitHub star growth highlights fast-rising repos, giving early insight ...
[Search Software Quality](https://www.techtarget.com/searchsoftwarequality/)
- [Harness Artifact Registry strengthens supply chain governance](https://www.techtarget.com/searchsoftwarequality/news/366639489/Harness-Artifact-Registry-strengthens-supply-chain-governance)
Harness makes its artifact registry generally available beyond early preview customers, with a security twist that could ...
- [AWS Kiro 'user error' reflects common AI coding review gap](https://www.techtarget.com/searchsoftwarequality/news/366639129/AWS-Kiro-user-error-reflects-common-AI-coding-review-gap)
Even internal AWS Kiro users haven't always peer-reviewed AI code output, as evidenced by a reported December outage that ...
- [AWS AI IDE, AgentCore throw down gauntlets for Microsoft](https://www.techtarget.com/searchsoftwarequality/news/366635669/AWS-AI-IDE-AgentCore-throw-down-gauntlets-for-Microsoft)
Kiro emerges as a significant alternative to GitHub Copilot agents, while AWS AgentCore updates square off against Agent 365 in ...
[Search Cloud Computing](https://www.techtarget.com/searchcloudcomputing/)
- [The hidden costs of technical debt in infrastructure](https://www.techtarget.com/searchcloudcomputing/tip/The-hidden-costs-of-technical-debt-in-infrastructure)
Technical debt is a strategic leadership issue rather than a budgeted maintenance concern. Orgs that actively manage technical ...
- [Is your cloud storage ready for AI workloads?](https://www.techtarget.com/searchcloudcomputing/tip/Is-your-cloud-storage-ready-for-AI-workloads)
Cloud storage design directly impacts AI performance, scalability and cost. Learn how to choose the right storage types, optimize...
- [State of the Cloud report shows shift from cost-cutting to value](https://www.techtarget.com/searchcloudcomputing/feature/State-of-the-Cloud-report-shows-shift-from-cost-cutting-to-value)
The CTO of Flexera shares his expert insights into the 2026 State of the Cloud Report’s findings and highlights a pivotal shift ...
[Search Security](https://www.techtarget.com/searchsecurity/)
- [News brief: Iranian cyberattacks target U.S. water, energy](https://www.techtarget.com/searchsecurity/news/366641657/News-brief-Iranian-cyberattacks-target-US-water-energy)
Check out the latest security news from TechTarget SearchSecurity's sister sites, Cybersecurity Dive and Dark Reading.
- [Human vs. AI: Debates shape RSAC 2026 cybersecurity trends](https://www.darkreading.com/cybersecurity-operations/human-vs-ai-debates-shape-rsac-2026-cybersecurity-trends)
As AI dominated RSAC 2026, CISOs and industry leaders debated its role in security, from agentic applications to the challenges ...
- [CISO checklist: Cybersecurity platform or marketing ploy?](https://www.techtarget.com/searchsecurity/feature/CISO-checklist-Cybersecurity-platform-or-marketing-ploy)
The cybersecurity market is booming with countless vendors claiming to offer unified platforms. Here's how to separate the real ...
[Search AWS](https://www.techtarget.com/searchaws/)
- [Compare Datadog vs. New Relic for IT monitoring in 2024](https://www.techtarget.com/searchaws/tip/Compare-CloudWatch-vs-Datadog-and-New-Relic-for-AWS-monitoring)
Compare Datadog vs. New Relic capabilities including alerts, log management, incident management and more. Learn which tool is ...
- [AWS Control Tower aims to simplify multi-account management](https://www.techtarget.com/searchaws/tip/AWS-Control-Tower-aims-to-simplify-multi-account-management)
Many organizations struggle to manage their vast collection of AWS accounts, but Control Tower can help. The service automates ...
- [Break down the Amazon EKS pricing model](https://www.techtarget.com/searchaws/tip/Compare-EKS-pricing-to-other-managed-Kubernetes-services)
There are several important variables within the Amazon EKS pricing model. Dig into the numbers to ensure you deploy the service ...
- Follow:
- [About Us](https://www.informatechtarget.com/editorial/)
- [Editorial Ethics Policy](https://www.techtarget.com/techtarget-editorial-ethics-policy)
- [Meet The Editors](https://www.techtarget.com/editorial-contacts)
- [Contact Us](https://www.informatechtarget.com/contact-us/)
- [Advertisers](https://www.informatechtarget.com/for-advertisers/)
- [Partner with Us](https://www.informatechtarget.com/partner-with-us/)
- [Media Kit](https://www.informatechtarget.com/wp-content/uploads/2023/09/TechTarget-Media-Kit-Handout-with-product-descriptions.pdf)
- [Corporate Site](https://www.informatechtarget.com/)
- [Contributors](https://www.theserverside.com/contributors)
- [Reprints](https://licensing.ygsgroup.com/ygspublishersolutions/)
- [Answers](https://www.theserverside.com/answers)
- [Definitions](https://www.theserverside.com/definitions)
- [E-Products](https://www.theserverside.com/eproducts)
- [Events](https://www.brighttalk.com/summits)
- [Features](https://www.theserverside.com/features)
- [Guides](https://www.theserverside.com/guides)
- [Opinions](https://www.theserverside.com/opinions)
- [Photo Stories](https://www.theserverside.com/photostories)
- [Quizzes](https://www.theserverside.com/quizzes)
- [Tips](https://www.theserverside.com/tips)
- [Tutorials](https://www.theserverside.com/tutorials)
- [Videos](https://www.theserverside.com/videos)
[©2026 TechTarget](https://www.informatechtarget.com/terms-of-use), Inc. d/b/a Informa TechTarget. All Rights Reserved.
[Privacy Policy](https://www.informatechtarget.com/privacy-policy/)
[Cookie Preferences](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example)
[Cookie Preferences](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example)
[Do Not Sell or Share My Personal Information](https://techtarget.zendesk.com/hc/en-us/requests/new?ticket_form_id=360004852434)
Close
 |
| Readable Markdown | The most misunderstood operation in the world of distributed version control must be the git revert command. Let's walk through an example of how to revert a Git commit, and differentiate the git reset and git revert commands.
The purpose of the git revert command is to remove all the changes a single commit made to your source code repository. For example, if a past commit added a file named index.html to the repo, a git revert on that commit will remove the index.html file from the repo. If a past commit added a new line of code to a Java file, a git revert on that commit will remove the added line.
When you revert a Git commit, the changes from the targeted commit are removed from your local workspace. A new commit is also created to reflect the new state of your repository.
## The git revert command
The syntax to revert a Git commit and undo unwanted changes is simple. All developers need to do is issue the git revert command and provide the ID of the commit to undo:
```
git@commit /c/revert example/
$ git revert 4945db2
```
## A git revert commit example
To really understand how to [undo Git commits](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/git-unstage-file-all-index-commit-folder-add-delete), look at this git revert example.
We will start with a [git init](https://www.theserverside.com/video/How-to-create-a-local-repository-with-the-git-init-command) command to create a completely clean repository:
```
git@commit /c/revert example/
$ git init
Initialized empty Git repo in C:/git revert example
```
With the repository initialized, we'll add five files to the repo. Each time a new file is created, we add it to the Git index and create a new commit with a meaningful message.
```
git@commit /c/revert example/
$ touch alpha.html
$ git add . && git commit -m "1st git commit: 1 file"
$ touch beta.html
$ git add . && git commit -m "2nd git commit: 2 files"
$ touch charlie.html
$ git add . && git commit -m "3rd git commit: 3 files"
$ touch delta.html
$ git add . && git commit -m "4th git commit: 4 files"
$ touch edison.html
$ git add . && git commit -m "5th git commit: 5 files"
```
A quick directory listing following the initial command batch shows five files in the current folder:
```
git@commit /c/revert example/
$ ls
alpha.html beta.html charlie.html delta.html edison.html
```
A call to the [git reflog command](https://www.theserverside.com/video/Git-reflog-vs-log-How-these-commit-history-tools-differ) will show us our current commit history:
```
git@commit /c/revert example/
$ git reflog
(HEAD -> master)
d846aa8 HEAD@{0}: commit: 5th git commit: 5 files
0c59891 HEAD@{1}: commit: 4th git commit: 4 files
4945db2 HEAD@{2}: commit: 3rd git commit: 3 files
defc4eb HEAD@{3}: commit: 2nd git commit: 2 files
2938ee3 HEAD@{4}: commit: 1st git commit: 1 file
```
## How to revert a Git commit
What do you think would happen if we did a git revert on the third commit with ID 4945db2? This was the git commit that added the charlie.html file.
```
git@commit /c/revert example/
$ git revert 4945db2
```
Will the git revert of commit 4945db2 remove charlie.html, delta.html and edison.html from the local workspace?
Will the git revert of commit 4945db2 remove delta.html and edison.html from the local workspace, but leave the other files alone?
Or will this git revert example leave four files in the local workspace and remove only the charlie.html file?
If you chose the last outcome, you'd be correct. Here's why.
The git revert command will undo only the changes associated with a specific commit. In this git revert example, the third commit added the charlie.html file. When we revert said Git commit, the only file removed from our repository is charlie.html.
```
git@commit /c/revert example/
$ ls
alpha.html beta.html delta.html edison.html
```
Developers also need to know that when they git revert a commit, the reverted commit is deleted from their local workspace, but not deleted from the local repository. The code associated with the reverted Git commit remains stored in the repository's history of changes, which means reverted code is still referenceable if it ever needs to be accessed or reviewed in the future.

Options like continue, abort and edit help to simplify the process of how to git revert a commit.
## Compare git revert vs. reset
When you git revert a commit, only the changes associated with that commit are undone. Cumulative changes from subsequent commits aren't affected. If you wish to undo every change since a given commit occurred, you'd want to issue a [hard git reset](https://www.theserverside.com/video/How-to-use-the-git-reset-hard-command-to-change-a-commit-history), not revert.
The git reset and revert commands are commonly confused, but they apply to distinct use cases. To undo changes associated with a specific commit, developers should use the git revert command. To [undo every change](https://www.theserverside.com/video/Dont-git-revert-that-last-commit-git-reset-instead) that has happened since a given commit occurred, use git reset. In the specific case where a developer needs to undo only the previous commit, either of the git revert or git reset commands will suffice.
## Steps to revert a Git commit
In review, the steps to git revert a commit and undo unwanted changes are the following:
1. Locate the ID of the commit to revert with the git log or reflog command.
2. Issue the git revert command and provide the commit ID of interest.
3. Supply a meaningful [Git commit message](https://www.theserverside.com/video/Follow-these-git-commit-message-guidelines) to describe why the revert was needed.
The git revert command is a simple way to remove a bug introduced to the version control system at some point in the past, or back out of a feature enhancement that wasn't well-received by the client. If you want to undo changes that happened in a specific commit, the git revert command is the correct operation to choose.
#### Next Steps
[Git fork vs. clone: What's the difference?](https://www.theserverside.com/answer/Git-fork-vs-clone-Whats-the-difference)
[Uncommit Git's last commit](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/uncommit-git-last-commit-file-changes-pushed-deleted-message)
[Git vs. GitHub: What is the difference between them?](https://www.theserverside.com/video/Git-vs-GitHub-What-is-the-difference-between-them)
#### Dig Deeper on DevOps-driven, cloud-native app development
- [ How to unstage a file in Git  By: Cameron McKenzie](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/git-unstage-file-all-index-commit-folder-add-delete)
- [ How to publish GitHub Actions artifacts by example  By: Cameron McKenzie](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-to-publish-GitHub-Actions-artifacts-example)
- [ How to git cherry-pick a commit example  By: Cameron McKenzie](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Need-to-git-cherry-pick-a-commit-Heres-an-example-how)
- [ Git reset hard vs. soft: What's the difference?  By: Cameron McKenzie](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Git-reset-hard-vs-soft-Whats-the-difference) |
| Shard | 23 (laksa) |
| Root Hash | 7434987543936799023 |
| Unparsed URL | com,theserverside!www,/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example s443 |