ℹ️ 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.techtarget.com/searchitoperations/answer/How-to-roll-back-Git-code-to-a-previous-commit |
| Last Crawled | 2026-04-13 11:17:04 (4 days ago) |
| First Indexed | 2022-03-15 00:29:55 (4 years ago) |
| HTTP Status Code | 200 |
| Meta Title | How to roll back Git code to a previous commit | TechTarget |
| Meta Description | When developers need a git rollback to a previous commit, there are two command options that can help: git reset and git revert. |
| Meta Canonical | null |
| Boilerpipe Text | With Git, IT teams can implement version control. Humans make mistakes and sometimes need to roll back to previous versions of content.
Luckily, there are mechanisms in Git that allow programmers to
roll back these commits
to a known-good version. However, there are potential code implications that can arise when teams execute these commands.
What is
git reset?
Every time an IT admin commits a Git deployment, that latest commit becomes the head, or tip, of the code tree -- in other words, the current version. The Git commit process provides a point-in-time snapshot (PIT snapshot) of the version-controlled files at the time of every change.
An administrator can roll back the code repository to a previous commit -- that point-in-time copy -- in several ways, depending on the end goal. One approach is the
git reset
command.
Before using this command, teams must understand what
git reset
does. Outcomes can vary between command uses and with which switches. Use the command with caution. There are
two modes for git reset
:
Git reset soft.
This mode resets the code tree's head to the designated former commit instance. All the files between that PIT snapshot and now are set to staged -- ready to commit but not yet committed. This is the default mode.
Git reset hard.
Use this mode with extreme caution because the changes can't be reverted. This command will
reset everything
, move the head back to the indicated commit version and
remove all changes added to the code tree
after that specific version number. In effect, the
git reset
command instantiates a "hard deletion" of all changes from now -- or point in time of code reversion -- to the designated former code commit. It resets the code tree to the version in question and deletes
unstaged files
.
Git reset
example
First, decide how far back to go into the
version history
. To view the previous commits, use the
git log --oneline
command. This command
provides the commit details
.
Anthony Howell
Figure 1. The code displays the git log output of previous commits after running the git log –-oneline command.
Once the team chooses a code version they want to revert their tree to, use the commit ID to execute the command. In the following example, a soft reset is used since
--
hard
is not specified. The code 3a96a8e represents the commit ID, gained from the git log output in Figure 1.
Anthony Howell
Figure 2. The code displays the commit ID 3a96a8e that will execute the revert.
Alternatively, there is a shorthand method to roll back the commit without knowing the necessary commit ID. Admins can reset code versions relative to where the current head is using the code in Figure 3.
In the example,
~1
refers to the number of commits backward that the code tree will revert to. The image below illustrates the results of adding several commits and then resetting back one version.
Anthony Howell
Figure 3. The code displays the output after running git reset head~1.
Git revert
example
Admins can also use
git revert
.
This command
undoes the effects of a bad or incorrect commit by creating a commit that does the opposite of the commit in question. By reverting the commit in this manner, the history stays intact.
Teams can use
git log --oneline
again to see the current commit IDs.
Anthony Howell
Figure 4. The code displays the current commit IDs after running git log –oneline.
Then they can revert to a specific commit with similar syntax as the reset command. However, the difference here is that the command specifies the commit ID that needs to be undone rather than the commit ID that the team wants to reset to.
Anthony Howell
Figure 5. The code displays the git history of the previous commit after running git revert 7b9a698.
In Figure 5 there's an additional parameter:
--no-edit
. This prevents Git from opening the default editor to specify a custom commit message.
Figure 6 displays how the git history will look.
Anthony Howell
Figure 6. The code displays the git history after reverting to the previous commit.
In this scenario, all changes made in the second commit were reverted with an additional commit that does the opposite.
Git reset
vs.
git revert
In choosing between
git reset
and
git revert
, it's important to understand exactly what each one does.
Git reset
will delete commits from the history, so if admins are working locally and haven't pushed any commits, this will likely be the better option.
But if the team has pushed the commits, consider
git revert
instead. That way, anyone who has pulled the bad commit can also
pull the revert commits
as well.
Before making any changes,
test these actions in a local copy of a repository
. That way, if admins make any mistakes,
they can fall back
to executing a
git pull
.
Editor's Note: This article was originally written by Stuart Burns in 2021. Anthony Howell revised and expanded upon it in 2023.
Dig Deeper on DevOps
How to discard local changes in Git
By:
Cameron McKenzie
Git reset hard vs. soft: What's the difference?
By:
Cameron McKenzie
How to amend a git commit
By:
Cameron McKenzie
Sourcetree tutorial for beginners
By:
Cameron McKenzie
Related Q&A from
Anthony Howell
Do-While vs. Do-Until in PowerShell: What's the difference?
PowerShell scripts have several loop options to make automation easy. Learn the difference between Do-While and Do-Until loops and choose which one ...
Â
Continue Reading
Manage the Windows PATH environment variable with PowerShell
Among many other tasks, IT teams can use PowerShell to manage and edit the PATH environment variable. Walk through these basic steps to access, add ...
Â
Continue Reading
PowerShell looping examples for the 4 key commands
There are many instances where looping is a valuable tool in your PowerShell script. Knowing how to use them will serve you well. Evaluate these four...
Â
Continue Reading |
| Markdown | [Search ITOperations](https://www.techtarget.com/)
[Login](https://www.techtarget.com/login?fromURL=https%3A%2F%2Fwww.techtarget.com%2Fsearchitoperations%2Fanswer%2FHow-to-roll-back-Git-code-to-a-previous-commit) [Register](https://www.techtarget.com/register?fromURL=https%3A%2F%2Fwww.techtarget.com%2Fsearchitoperations%2Fanswer%2FHow-to-roll-back-Git-code-to-a-previous-commit)
[Informa TechTarget](https://www.informatechtarget.com/) [Explore the Network](https://www.techtarget.com/searchitoperations/answer/How-to-roll-back-Git-code-to-a-previous-commit)
- [Software Quality](https://www.techtarget.com/searchsoftwarequality/)
- [Cloud Computing](https://www.techtarget.com/searchcloudcomputing/)
- [Data Center](https://www.techtarget.com/searchdatacenter/)
- [Data Center Knowledge](https://www.datacenterknowledge.com/)
- [Information Week](https://www.informationweek.com/)
[An Informa TechTarget Publication](https://www.informatechtarget.com/) [Explore our brands](https://www.informatechtarget.com/our-brands)
- [Search ITOperations](https://www.techtarget.com/searchitoperations/)
- - [Automation & Orchestration](https://www.techtarget.com/searchitoperations/resources/Systems-automation-and-orchestration)
- [Careers & Skills](https://www.techtarget.com/searchitoperations/resources/IT-operations-careers-and-skills)
- [Containers & Virtualization](https://www.techtarget.com/searchitoperations/resources/Containers-and-virtualization)
- [DevOps Tech & Culture](https://www.techtarget.com/searchitoperations/resources/DevOps)
- [IT Systems Management](https://www.techtarget.com/searchitoperations/resources/IT-Systems-Management-and-Monitoring)
[Other Content](https://www.techtarget.com/searchitoperations/answer/How-to-roll-back-Git-code-to-a-previous-commit)
- [News](https://www.techtarget.com/news/)
- [Features](https://www.techtarget.com/searchitoperations/features)
- [Tips](https://www.techtarget.com/searchitoperations/tips)
- [Webinars](https://www.techtarget.com/searchitoperations/webinars)
- [Sponsored Sites](https://www.techtarget.com/searchitoperations/sponsored_communities)
- [More](https://www.techtarget.com/searchitoperations/answer/How-to-roll-back-Git-code-to-a-previous-commit)
- [Answers](https://www.techtarget.com/searchitoperations/answers)
- [Conference Guides](https://www.techtarget.com/searchitoperations/guides)
- [Definitions](https://www.techtarget.com/searchitoperations/definitions)
- [Opinions](https://www.techtarget.com/searchitoperations/opinions)
- [Podcasts](https://www.techtarget.com/searchitoperations/podcasts)
- [Quizzes](https://www.techtarget.com/searchitoperations/quizzes)
- [Tech Accelerators](https://www.techtarget.com/searchitoperations/techaccelerators)
- [Tutorials](https://www.techtarget.com/searchitoperations/tutorials)
- [Videos](https://www.techtarget.com/searchitoperations/videos)
- [Home](https://www.techtarget.com/searchitoperations/)
- [DevOps](https://www.techtarget.com/searchitoperations/resources/DevOps)

Getty Images/iStockphoto
Getty Images/iStockphoto
[Answer](https://www.techtarget.com/searchitoperations/answers)
# How to roll back Git code to a previous commit
## Git reset and git revert can be lifesavers for developers. Follow these examples to see which command makes the most sense when mistakes arise during development.
- Share this item with your network:
By
- [Anthony Howell](https://www.techtarget.com/contributor/Anthony-Howell)
- [Stuart Burns](https://www.techtarget.com/contributor/Stuart-Burns)
Published: 14 Aug 2023
With Git, IT teams can implement version control. Humans make mistakes and sometimes need to roll back to previous versions of content.
Luckily, there are mechanisms in Git that allow programmers to [roll back these commits](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/amend-git-commit-message-remove-undo-change-update-github-gitlab) to a known-good version. However, there are potential code implications that can arise when teams execute these commands.
## What is git reset?
Every time an IT admin commits a Git deployment, that latest commit becomes the head, or tip, of the code tree -- in other words, the current version. The Git commit process provides a point-in-time snapshot (PIT snapshot) of the version-controlled files at the time of every change.
An administrator can roll back the code repository to a previous commit -- that point-in-time copy -- in several ways, depending on the end goal. One approach is the git reset command.
Before using this command, teams must understand what git reset does. Outcomes can vary between command uses and with which switches. Use the command with caution. There are [two modes for git reset](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Git-reset-hard-vs-soft-Whats-the-difference):
- Git reset soft. This mode resets the code tree's head to the designated former commit instance. All the files between that PIT snapshot and now are set to staged -- ready to commit but not yet committed. This is the default mode.
- Git reset hard. Use this mode with extreme caution because the changes can't be reverted. This command will [reset everything](https://www.theserverside.com/video/How-to-use-the-git-reset-hard-command-to-change-a-commit-history), move the head back to the indicated commit version and [remove all changes added to the code tree](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/remove-revert-discard-local-uncommitted-changes-Git-how-to) after that specific version number. In effect, the git reset command instantiates a "hard deletion" of all changes from now -- or point in time of code reversion -- to the designated former code commit. It resets the code tree to the version in question and deletes [unstaged files](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/git-unstage-file-all-index-commit-folder-add-delete).
## Git reset example
First, decide how far back to go into the [version history](https://www.theserverside.com/video/Understand-the-Git-working-tree-status-command-for-easy-DVCS). To view the previous commits, use the git log --oneline command. This command [provides the commit details](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-to-git-stash-untracked-files).

Anthony Howell
Figure 1. The code displays the git log output of previous commits after running the git log –-oneline command.
Once the team chooses a code version they want to revert their tree to, use the commit ID to execute the command. In the following example, a soft reset is used since \--hard is not specified. The code 3a96a8e represents the commit ID, gained from the git log output in Figure 1.

Anthony Howell
Figure 2. The code displays the commit ID 3a96a8e that will execute the revert.
Alternatively, there is a shorthand method to roll back the commit without knowing the necessary commit ID. Admins can reset code versions relative to where the current head is using the code in Figure 3.
In the example, ~1 refers to the number of commits backward that the code tree will revert to. The image below illustrates the results of adding several commits and then resetting back one version.

Anthony Howell
Figure 3. The code displays the output after running git reset head~1.
## Git revert example
Admins can also use git revert. [This command](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example) undoes the effects of a bad or incorrect commit by creating a commit that does the opposite of the commit in question. By reverting the commit in this manner, the history stays intact.
Teams can use git log --oneline again to see the current commit IDs.

Anthony Howell
Figure 4. The code displays the current commit IDs after running git log –oneline.
Then they can revert to a specific commit with similar syntax as the reset command. However, the difference here is that the command specifies the commit ID that needs to be undone rather than the commit ID that the team wants to reset to.

Anthony Howell
Figure 5. The code displays the git history of the previous commit after running git revert 7b9a698.
In Figure 5 there's an additional parameter: \--no-edit. This prevents Git from opening the default editor to specify a custom commit message.
Figure 6 displays how the git history will look.

Anthony Howell
Figure 6. The code displays the git history after reverting to the previous commit.
In this scenario, all changes made in the second commit were reverted with an additional commit that does the opposite.
## Git reset vs. git revert
In choosing between git reset and git revert, it's important to understand exactly what each one does. Git reset will delete commits from the history, so if admins are working locally and haven't pushed any commits, this will likely be the better option.
But if the team has pushed the commits, consider git revert instead. That way, anyone who has pulled the bad commit can also [pull the revert commits](https://www.theserverside.com/video/Undo-and-revert-pushed-Git-commits) as well.
Before making any changes, [test these actions in a local copy of a repository](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/delete-local-git-branch-origin-force-merge-all). That way, if admins make any mistakes, [they can fall back](https://git-scm.com/docs/git-pull) to executing a git pull.
*Editor's Note: This article was originally written by Stuart Burns in 2021. Anthony Howell revised and expanded upon it in 2023.*
#### Related Resources
- [The Future of CNAPP: Operationalizing Cloud Security in 2026](https://www.brighttalk.com/webcast/11673/666380) –Talk
- [Simplify Hybrid Infrastructure with Autonomous Cloud and Zero-Trust Strategies](https://www.brighttalk.com/webcast/679/659036) –Talk
- [CPE \| Next-Gen PAM: Automating Privileged Access for Operational Risk in Hybrid...](https://www.brighttalk.com/webcast/5586/664134) –Talk
- [Ep.3-Build series: How to Deploy Distributed AI at Global Scale](https://www.brighttalk.com/webcast/17473/665918) –Akamai APAC Cloud
#### Dig Deeper on DevOps
- [ How to discard local changes in Git  By: Cameron McKenzie](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/remove-revert-discard-local-uncommitted-changes-Git-how-to)
- [ 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)
- [ How to amend a git commit  By: Cameron McKenzie](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/amend-git-commit-message-remove-undo-change-update-github-gitlab)
- [ Sourcetree tutorial for beginners  By: Cameron McKenzie](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/sourcetree-tutorial-bitbucket-git-course-branch-merge-conflict-install-download-rebase-reset)
#### Related Q\&A from [Anthony Howell](https://www.techtarget.com/contributor/Anthony-Howell)
##### [Do-While vs. Do-Until in PowerShell: What's the difference?](https://www.techtarget.com/searchitoperations/answer/Do-While-vs-Do-Until-in-PowerShell-Whats-the-difference)
PowerShell scripts have several loop options to make automation easy. Learn the difference between Do-While and Do-Until loops and choose which one ... [Continue Reading](https://www.techtarget.com/searchitoperations/answer/Do-While-vs-Do-Until-in-PowerShell-Whats-the-difference)
##### [Manage the Windows PATH environment variable with PowerShell](https://www.techtarget.com/searchitoperations/answer/Manage-the-Windows-PATH-environment-variable-with-PowerShell)
Among many other tasks, IT teams can use PowerShell to manage and edit the PATH environment variable. Walk through these basic steps to access, add ... [Continue Reading](https://www.techtarget.com/searchitoperations/answer/Manage-the-Windows-PATH-environment-variable-with-PowerShell)
##### [PowerShell looping examples for the 4 key commands](https://www.techtarget.com/searchitoperations/answer/PowerShell-looping-examples-for-the-4-key-commands)
There are many instances where looping is a valuable tool in your PowerShell script. Knowing how to use them will serve you well. Evaluate these four... [Continue Reading](https://www.techtarget.com/searchitoperations/answer/PowerShell-looping-examples-for-the-4-key-commands)
Sponsored News
- [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
- [How advanced AI-powered digital assistants can improve physician documentation](https://www.techtarget.com/searchhealthit/Oracle-Healthcare/How-advanced-AI-powered-digital-assistants-can-improve-physician-documentation) –Oracle Healthcare
- [How Gaming Innovation Is Accelerating Manufacturing's Digital Transformation](https://www.techtarget.com/searchcio/UnityTech/How-Gaming-Innovation-Is-Accelerating-Manufacturings-Digital-Transformation) –Unity Technologies
- [See More](https://www.techtarget.com/searchitoperations/sponsored_communities)
Related Expert Q\&A
- [Explore the revert-to-snapshot feature in OpenStack ...](https://www.techtarget.com/searchcloudcomputing/answer/Explore-the-revert-to-snapshot-feature-in-OpenStack-Cinder) – Search Cloud Computing
- [Can a VMware snapshot resurrect a VMDK file?](https://www.techtarget.com/searchvmware/answer/Can-a-VMware-snapshot-resurrect-a-VMDK-file) – Search VMware
- [How does BrickerBot threaten enterprise IoT devices?](https://www.techtarget.com/searchsecurity/answer/How-does-BrickerBot-threaten-enterprise-IoT-devices) – Search Security
Latest TechTarget resources
- [Software Quality](https://www.techtarget.com/searchitoperations/answer/How-to-roll-back-Git-code-to-a-previous-commit)
- [Cloud Computing](https://www.techtarget.com/searchitoperations/answer/How-to-roll-back-Git-code-to-a-previous-commit)
- [Data Center](https://www.techtarget.com/searchitoperations/answer/How-to-roll-back-Git-code-to-a-previous-commit)
[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 Data Center](https://www.techtarget.com/searchdatacenter/)
- [Modern data center sustainability: 5 best practices to consider](https://www.techtarget.com/searchdatacenter/tip/Modern-data-center-sustainability-best-practices-to-consider)
Sustainable data centers are needed as energy demands rise. CIOs should consider the following best practices to reduce costs, ...
- [From core to edge: Strategies for scalable, compliant and agile IT](https://www.techtarget.com/searchdatacenter/tip/From-core-to-edge-Strategies-for-scalable-compliant-and-agile-IT)
Edge computing is revolutionizing data processing, allowing organizations to reduce latency and enhance real-time insights. ...
- [How to secure the data center's expanding attack surface](https://www.techtarget.com/searchdatacenter/tip/How-to-secure-the-data-centers-expanding-attack-surface)
CIOs must adapt to an expanding data center attack surface by securing all IT access points and adopting zero trust networks and ...
- 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.techtarget.com/searchitoperations/contributors)
- [Reprints](https://licensing.ygsgroup.com/ygspublishersolutions/)
- [Answers](https://www.techtarget.com/searchitoperations/answers)
- [Definitions](https://www.techtarget.com/searchitoperations/definitions)
- [E-Products](https://www.techtarget.com/searchitoperations/eproducts)
- [Events](https://www.brighttalk.com/summits)
- [Features](https://www.techtarget.com/searchitoperations/features)
- [Guides](https://www.techtarget.com/searchitoperations/guides)
- [Opinions](https://www.techtarget.com/searchitoperations/opinions)
- [Photo Stories](https://www.techtarget.com/searchitoperations/photostories)
- [Quizzes](https://www.techtarget.com/searchitoperations/quizzes)
- [Tips](https://www.techtarget.com/searchitoperations/tips)
- [Tutorials](https://www.techtarget.com/searchitoperations/tutorials)
- [Videos](https://www.techtarget.com/searchitoperations/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.techtarget.com/searchitoperations/answer/How-to-roll-back-Git-code-to-a-previous-commit)
[Cookie Preferences](https://www.techtarget.com/searchitoperations/answer/How-to-roll-back-Git-code-to-a-previous-commit)
[Do Not Sell or Share My Personal Information](https://techtarget.zendesk.com/hc/en-us/requests/new?ticket_form_id=360004852434)
Close
 |
| Readable Markdown | With Git, IT teams can implement version control. Humans make mistakes and sometimes need to roll back to previous versions of content.
Luckily, there are mechanisms in Git that allow programmers to [roll back these commits](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/amend-git-commit-message-remove-undo-change-update-github-gitlab) to a known-good version. However, there are potential code implications that can arise when teams execute these commands.
## What is git reset?
Every time an IT admin commits a Git deployment, that latest commit becomes the head, or tip, of the code tree -- in other words, the current version. The Git commit process provides a point-in-time snapshot (PIT snapshot) of the version-controlled files at the time of every change.
An administrator can roll back the code repository to a previous commit -- that point-in-time copy -- in several ways, depending on the end goal. One approach is the git reset command.
Before using this command, teams must understand what git reset does. Outcomes can vary between command uses and with which switches. Use the command with caution. There are [two modes for git reset](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Git-reset-hard-vs-soft-Whats-the-difference):
- Git reset soft. This mode resets the code tree's head to the designated former commit instance. All the files between that PIT snapshot and now are set to staged -- ready to commit but not yet committed. This is the default mode.
- Git reset hard. Use this mode with extreme caution because the changes can't be reverted. This command will [reset everything](https://www.theserverside.com/video/How-to-use-the-git-reset-hard-command-to-change-a-commit-history), move the head back to the indicated commit version and [remove all changes added to the code tree](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/remove-revert-discard-local-uncommitted-changes-Git-how-to) after that specific version number. In effect, the git reset command instantiates a "hard deletion" of all changes from now -- or point in time of code reversion -- to the designated former code commit. It resets the code tree to the version in question and deletes [unstaged files](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/git-unstage-file-all-index-commit-folder-add-delete).
## Git reset example
First, decide how far back to go into the [version history](https://www.theserverside.com/video/Understand-the-Git-working-tree-status-command-for-easy-DVCS). To view the previous commits, use the git log --oneline command. This command [provides the commit details](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-to-git-stash-untracked-files).

Anthony Howell
Figure 1. The code displays the git log output of previous commits after running the git log –-oneline command.
Once the team chooses a code version they want to revert their tree to, use the commit ID to execute the command. In the following example, a soft reset is used since \--hard is not specified. The code 3a96a8e represents the commit ID, gained from the git log output in Figure 1.

Anthony Howell
Figure 2. The code displays the commit ID 3a96a8e that will execute the revert.
Alternatively, there is a shorthand method to roll back the commit without knowing the necessary commit ID. Admins can reset code versions relative to where the current head is using the code in Figure 3.
In the example, ~1 refers to the number of commits backward that the code tree will revert to. The image below illustrates the results of adding several commits and then resetting back one version.

Anthony Howell
Figure 3. The code displays the output after running git reset head~1.
## Git revert example
Admins can also use git revert. [This command](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example) undoes the effects of a bad or incorrect commit by creating a commit that does the opposite of the commit in question. By reverting the commit in this manner, the history stays intact.
Teams can use git log --oneline again to see the current commit IDs.

Anthony Howell
Figure 4. The code displays the current commit IDs after running git log –oneline.
Then they can revert to a specific commit with similar syntax as the reset command. However, the difference here is that the command specifies the commit ID that needs to be undone rather than the commit ID that the team wants to reset to.

Anthony Howell
Figure 5. The code displays the git history of the previous commit after running git revert 7b9a698.
In Figure 5 there's an additional parameter: \--no-edit. This prevents Git from opening the default editor to specify a custom commit message.
Figure 6 displays how the git history will look.

Anthony Howell
Figure 6. The code displays the git history after reverting to the previous commit.
In this scenario, all changes made in the second commit were reverted with an additional commit that does the opposite.
## Git reset vs. git revert
In choosing between git reset and git revert, it's important to understand exactly what each one does. Git reset will delete commits from the history, so if admins are working locally and haven't pushed any commits, this will likely be the better option.
But if the team has pushed the commits, consider git revert instead. That way, anyone who has pulled the bad commit can also [pull the revert commits](https://www.theserverside.com/video/Undo-and-revert-pushed-Git-commits) as well.
Before making any changes, [test these actions in a local copy of a repository](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/delete-local-git-branch-origin-force-merge-all). That way, if admins make any mistakes, [they can fall back](https://git-scm.com/docs/git-pull) to executing a git pull.
*Editor's Note: This article was originally written by Stuart Burns in 2021. Anthony Howell revised and expanded upon it in 2023.*
#### Dig Deeper on DevOps
- [ How to discard local changes in Git  By: Cameron McKenzie](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/remove-revert-discard-local-uncommitted-changes-Git-how-to)
- [ 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)
- [ How to amend a git commit  By: Cameron McKenzie](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/amend-git-commit-message-remove-undo-change-update-github-gitlab)
- [ Sourcetree tutorial for beginners  By: Cameron McKenzie](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/sourcetree-tutorial-bitbucket-git-course-branch-merge-conflict-install-download-rebase-reset)
#### Related Q\&A from [Anthony Howell](https://www.techtarget.com/contributor/Anthony-Howell)
##### [Do-While vs. Do-Until in PowerShell: What's the difference?](https://www.techtarget.com/searchitoperations/answer/Do-While-vs-Do-Until-in-PowerShell-Whats-the-difference)
PowerShell scripts have several loop options to make automation easy. Learn the difference between Do-While and Do-Until loops and choose which one ... [Continue Reading](https://www.techtarget.com/searchitoperations/answer/Do-While-vs-Do-Until-in-PowerShell-Whats-the-difference)
##### [Manage the Windows PATH environment variable with PowerShell](https://www.techtarget.com/searchitoperations/answer/Manage-the-Windows-PATH-environment-variable-with-PowerShell)
Among many other tasks, IT teams can use PowerShell to manage and edit the PATH environment variable. Walk through these basic steps to access, add ... [Continue Reading](https://www.techtarget.com/searchitoperations/answer/Manage-the-Windows-PATH-environment-variable-with-PowerShell)
##### [PowerShell looping examples for the 4 key commands](https://www.techtarget.com/searchitoperations/answer/PowerShell-looping-examples-for-the-4-key-commands)
There are many instances where looping is a valuable tool in your PowerShell script. Knowing how to use them will serve you well. Evaluate these four... [Continue Reading](https://www.techtarget.com/searchitoperations/answer/PowerShell-looping-examples-for-the-4-key-commands) |
| Shard | 182 (laksa) |
| Root Hash | 17789036595607440382 |
| Unparsed URL | com,techtarget!www,/searchitoperations/answer/How-to-roll-back-Git-code-to-a-previous-commit s443 |