🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 95 (from laksa198)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

đź“„
INDEXABLE
âś…
CRAWLED
1 month ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH1.1 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://www.linode.com/docs/guides/revert-last-git-commit/
Last Crawled2026-03-05 20:57:46 (1 month ago)
First Indexed2022-10-29 04:49:57 (3 years ago)
HTTP Status Code200
Meta TitleHow to Revert the Last Commit in Git | Linode Docs
Meta DescriptionMistakes happen, and the Git version control system has tools to help you navigate them. In this tutorial, learn two methods to undo your most recent Git commit, what sets the methods apart, and when to use them.
Meta Canonicalnull
Boilerpipe Text
Git is a widely used Version Control System (VCS) known for its versatility. It utilizes local clones of central repositories and keeps track of every commit so changes can be rolled back as needed. You might need that recoverability after an inadvertent commit or to undo a commit for any reason. This tutorial shows you how to use the git command line utility to revert a commit. It covers methods using both the git revert and git reset commands and explains the differences. Learn more about Git generally in our guide Git vs SVN: Pros and Cons of Each Version Control System . For a more general, and thorough, coverage of reverting Git commits, take a look at our guide on How to Undo a Git Commit . Optional: Create a Test Repository If you’d like to test reverting and resetting in a separate repository from one you actively work in, follow the steps below. This will set up an example Git repository similar to the one used for the examples in this tutorial. The commit IDs may be different, but the contents of the repository should otherwise be the same. The steps presume you have already installed Git and done basic configuration (e.g. user email address and name). If you have not done this yet, you can learn how in our guide How to Install Git and Clone a GitHub Repository . Create a new directory for your Git repository, and change it into that directory. Here, the new directory, git-example is created in the current user’s home directory. mkdir ~/git-example cd ~/git-example From here on, you should execute the given commands while working in this directory. Initialize the new Git repository. git init Use the touch command to create some new empty files. touch example-file-1.txt touch example-file-2.txt Add the files to the Git staging area, then commit the staged changes. git add . git commit -m "Initialized repo." Make some changes to the first file, adding some content to it. Then stage and commit those changes using the following commands: Do the same for the second file. You now have a Git repository with a couple of files and several commits, which you can see listed with the following command: git log --oneline The output of the command looks like the following: f4391b2 (HEAD -> master) Added text to second file. e3c534a Added text to first file. 0b24777 Initialized repo. How to Use the git revert Command on the Last Commit Git revert undoes a commit by comparing the changes made in that commit to the repository’s previous state. It then creates a new commit that reverts the changes. To use the git revert command, you first need the ID for that commit. You can get this with the git log command. Here, the command is used with the --oneline option to make each commit display on a single line: git log --oneline f4391b2 (HEAD -> master) Added text to second file. e3c534a Added text to first file. 0b24777 Initialized repo. The line at the top of the output, with an ID of f4391b2 in this example, represents the last commit. You can revert that commit using the revert command with that commit’s ID, as shown here: git revert f4391b2 Git starts a new commit to revert the changes. It may present you with a text editor allowing you to edit the description for the new commit. When you are satisfied with the description, exit the text editor to complete the reversion. [master e86542a] Revert "Added text to second file." 1 file changed, 1 deletion(-) Running the git log command again shows that there is now a new commit to revert the previous commit: git log --oneline e86542a (HEAD -> master) Revert "Added text to second file." f4391b2 Added text to second file. e3c534a Added text to first file. 0b24777 Initialized repo. If you view the affected file, you can see that the changes have been reversed. In this case, that is example-file-2.txt : cat example-file-2.txt How to Use the git reset Command to Undo Commits Git reset can also be used to revert the last commit. The command is more powerful than git revert and works by removing commits entirely from the repository’s commit history. Essentially, reset “rewinds” you to a previous commit, eliminating later commits and history along the way. With the git reset command, you have access to the HEAD~1 alias. This alias stands in for the ID of the previous commit, providing easy access when trying to revert to the last commit. The git reset command comes with three flags that define how the command deals with changed files in the working directory and staging area. Use the --soft option to roll back to a previous commit, while preserving file changes in the working directory and staging area. git reset --soft HEAD~1 Use the --hard option to likewise roll back to a previous commit. However, this option results in all file changes being reverted as well. Changes to files in the working directory as well as staged changes are both discarded. git reset --hard HEAD~1 Use the --mixed option to, again, roll back to a previous commit. Here, a middle ground is adopted for file changes. As with the --hard option, changes made to files in the working directory are discarded. However, like the --soft option, staged changes are preserved. git reset --mixed HEAD~1 After any of the above commands, you can see that the last commit has been removed from the Git commit history: git log --oneline e3c534a (HEAD -> master) Added text to first file. 0b24777 Initialized repo. Differences Between Reset and Revert The main difference between the git reset and git revert commands is the commit history. Revert aims to maintain a full commit history, undoing a commit by assessing changes and making a new commit that reverses them. This actually means that this command adds to the commit history. It gives you full transparency to past commits and their reversions. In contrast, using reset discards commits even from the commit history, making them impossible to recover later. The git reset command can even do the same for local file changes. This option keeps you from seeing reversions and the original commits that are reverted. Generally, it is recommended that you use git revert for backing out a commit. It keeps a record of the removed commit and leaves the possibility of assessing and accessing the commit history later. The git reset command should be used sparingly. Take a good look at the situation to ensure that it cannot be remedied by reverting a commit instead of resetting to a previous commit. A reset might be preferred, however, for immediately undoing mistaken commits, when there is less chance that you need options for recovering the changes. Conclusion That covers all you need to revert recent Git commits. Moreover, the techniques covered in this tutorial can also help you manage Git commits more generally. The git revert command can be useful for precisely removing past commits while retaining your commit history. The git reset command, on the other hand, provides a more radical option, completely reverting a repository to a previous commit, including the commit history. To keep learning, refer to the links at the beginning of this guide. These give you more on Git generally as well as more on the commands covered in this tutorial. You may also want to look at our entire lineup of guides on version control . These cover everything from the fundamentals to particular use cases, and provide steps to deepen your version control knowledge. More Information You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials. freeCodeCamp: Git Revert Commit – How to Undo the Last Commit TechTarget: How to Revert a Git Commit This page was originally published on August 18, 2022.
Markdown
- [Search](https://www.linode.com/search/) - [Docs](https://techdocs.akamai.com/cloud-computing/docs/welcome) - [Blog](https://www.linode.com/blog/) - [Support](https://www.linode.com/support/) - [Sales](https://www.linode.com/company/contact/) - [Log in](https://login.linode.com/login) [![Akamai Cloud](https://www.linode.com/linode/en/images/logo/akamai-cloud-logo.svg)](https://www.linode.com/) - Explore - Products - Solutions - Pricing - Resources - [Partners](https://www.linode.com/partners/) - [Sign up](https://login.linode.com/signup) - Mobile - Explore - [Lower costs](https://www.linode.com/reduce-cloud-costs/) - [Lower latency](https://www.linode.com/low-latency-applications/) - [Cloud portability](https://www.linode.com/cloud-portability/) - [Global scale](https://www.linode.com/cloud-global-scale/) - [Our platform](https://www.linode.com/global-infrastructure/) - [Support experience](https://www.linode.com/support-experience/) - Products - [Products Overview](https://www.linode.com/products/) - Compute - [Essential Compute](https://www.linode.com/products/essential-compute/) - [GPU](https://www.linode.com/products/gpu/) - [Accelerated Compute](https://www.linode.com/products/accelerated-compute/) - [Kubernetes](https://www.linode.com/products/kubernetes/) - [App Platform](https://www.linode.com/products/app-platform/) - Storage - [Block Storage](https://www.linode.com/products/block-storage/) - [Object Storage](https://www.linode.com/products/object-storage/) - [Backups](https://www.linode.com/products/backups/) - Networking - [Cloud Firewall](https://www.linode.com/products/cloud-firewall/) - [DNS Manager](https://www.linode.com/products/dns-manager/) - [NodeBalancers](https://www.linode.com/products/nodebalancers/) - [Private Networking](https://www.linode.com/products/private-networking/) - Databases - [Managed Databases](https://www.linode.com/products/databases/) - Services - [Linode Managed](https://www.linode.com/products/managed/) - Solutions - [AI inferencing](https://www.linode.com/solutions/ai-inferencing/) - [Ecommerce](https://www.linode.com/solutions/ecommerce/) - [Gaming](https://www.linode.com/solutions/gaming/) - [Managed hosting](https://www.linode.com/solutions/managed-hosting/) - [Media](https://www.linode.com/solutions/media/) - [SaaS](https://www.linode.com/solutions/saas/) - Pricing - [Pricing list](https://www.linode.com/pricing/) - [Cloud computing calculator](https://www.linode.com/pricing/cloud-cost-calculator/) - Resources - [White paper, ebooks, videos…](https://www.linode.com/resources/) - [Developer hub](https://www.linode.com/developers/) - [Documentation](https://techdocs.akamai.com/cloud-computing/docs/welcome) - [Guides](https://www.linode.com/docs/) - [Reference architectures](https://www.linode.com/docs/reference-architecture/) - [Community Q\&A](https://www.linode.com/community/questions/) - [Beta program](https://www.linode.com/green-light/) - [Startup program](https://www.linode.com/startup-programs/) - [Marketplace apps](https://www.linode.com/marketplace/) - [Blog](https://www.linode.com/blog/) - [Events and workshops](https://www.linode.com/events/) - [Partners](https://www.linode.com/partners/) - Company - [About Us](https://www.linode.com/company/about/) - [Newsroom](https://www.akamai.com/newsroom) - [Careers](https://www.akamai.com/careers) - [Legal](https://www.linode.com/legal/) - [Sales](https://www.linode.com/company/contact/) - [System Status](https://status.linode.com/) - Search - [Docs](https://techdocs.akamai.com/cloud-computing/docs/welcome) - [Blog](https://www.linode.com/blog/) - [Resources](https://www.linode.com/resources/) - [Pricing](https://www.linode.com/pricing/) - [Support](https://www.linode.com/support/) - [Sales](https://www.linode.com/company/contact/) - [Log in](https://login.linode.com/login) - [Sign up](https://login.linode.com/signup) ###### Featured [![Hero Forge Craft of Code Header](https://www.linode.com/ns/features/prod/images/hero-forge-craft-of-code-632x279.jpg)Hero Forge Secure, accessible GPU instances. Unleash millions of adventures. Read story](https://www.akamai.com/resources/customer-story/hero-forge) - [Lower costs](https://www.linode.com/reduce-cloud-costs/) - [Lower latency](https://www.linode.com/low-latency-applications/) - [Cloud portability](https://www.linode.com/cloud-portability/) - [Global scale](https://www.linode.com/cloud-global-scale/) - [Our platform](https://www.linode.com/global-infrastructure/) - [Support experience](https://www.linode.com/support-experience/) ###### Featured [![GPU acceleration](https://www.linode.com/ns/features/prod/images/gpu-438.png)GPU acceleration Power AI and media workloads with on-demand NVIDIA™ GPUs at cost-effective pricing. View product details](https://www.linode.com/products/gpu/) [View all products](https://www.linode.com/products/) ###### Compute - [Essential Compute](https://www.linode.com/products/essential-compute/) - [GPU](https://www.linode.com/products/gpu/) - [Accelerated Compute](https://www.linode.com/products/accelerated-compute/) - [Kubernetes](https://www.linode.com/products/kubernetes/) - [App Platform](https://www.linode.com/products/app-platform/) ###### Storage - [Block Storage](https://www.linode.com/products/block-storage/) - [Object Storage](https://www.linode.com/products/object-storage/) - [Backups](https://www.linode.com/products/backups/) ###### Networking - [Cloud Firewall](https://www.linode.com/products/cloud-firewall/) - [DNS Manager](https://www.linode.com/products/dns-manager/) - [NodeBalancers](https://www.linode.com/products/nodebalancers/) - [Private Networking](https://www.linode.com/products/private-networking/) ###### Databases - [Managed Databases](https://www.linode.com/products/databases/) ###### Services - [Linode Managed](https://www.linode.com/products/managed/) ###### Featured [![Portability in the Cloud: Best Practices for Building SaaS-Based Applications](https://www.linode.com/ns/features/prod/images/MRK-4404-Portability-i632x217-px.jpg)Portability in the cloud Best practices for building SaaS-based applications. Get your copy](https://www.linode.com/resources/ebook/cloud-portability-saas-applications/) ###### Solutions - [AI inferencing](https://www.linode.com/solutions/ai-inferencing/) - [Ecommerce](https://www.linode.com/solutions/ecommerce/) - [Gaming](https://www.linode.com/solutions/gaming/) - [Managed hosting](https://www.linode.com/solutions/managed-hosting/) - [Media](https://www.linode.com/solutions/media/) - [SaaS](https://www.linode.com/solutions/saas/) ###### Featured [![Cloud Pricing Calculator](https://www.linode.com/ns/features/prod/images/Cloud_Pricing_MockUp2-416x376.png)Cloud computing calculator Estimate your cloud costs. Price and configure cloud features to match your needs. Launch calculator](https://www.linode.com/pricing/cloud-cost-calculator/) ###### Pricing - [Pricing list](https://www.linode.com/pricing/) - [Cloud computing calculator](https://www.linode.com/pricing/cloud-cost-calculator/) ###### Featured [![](https://www.linode.com/ns/features/prod/images/developerHub-submenu-sm.jpg)Developer hub Comprehensive guides, documentation, and endpoints. Everything you need to get started. Visit hub](https://www.linode.com/developers/) ###### Library - [White paper, ebooks, videos…](https://www.linode.com/resources/) ###### Technical Resources - [Developer hub](https://www.linode.com/developers/) - [Documentation](https://techdocs.akamai.com/cloud-computing/docs/welcome) - [Guides](https://www.linode.com/docs/) - [Reference architectures](https://www.linode.com/docs/reference-architecture/) ###### Community - [Community Q\&A](https://www.linode.com/community/questions/) - [Beta program](https://www.linode.com/green-light/) - [Startup program](https://www.linode.com/startup-programs/) ###### Marketplace - [Browse apps](https://www.linode.com/marketplace/) ###### What's New - [Blog](https://www.linode.com/blog/) - [Events and workshops](https://www.linode.com/events/) Explore docs 1. [Docs Home](https://www.linode.com/docs/) 2. [Guides](https://www.linode.com/docs/guides/) 3. [Development](https://www.linode.com/docs/guides/development/) 4. [Version Control](https://www.linode.com/docs/guides/development/version-control/) - On this page On this page ### On this page 1. [Optional: Create a Test Repository](https://www.linode.com/docs/guides/revert-last-git-commit/#optional-create-a-test-repository) 2. [How to Use the git revert Command on the Last Commit](https://www.linode.com/docs/guides/revert-last-git-commit/#how-to-use-the-git-revert-command-on-the-last-commit) 3. [How to Use the git reset Command to Undo Commits](https://www.linode.com/docs/guides/revert-last-git-commit/#how-to-use-the-git-reset-command-to-undo-commits) 1. [Differences Between Reset and Revert](https://www.linode.com/docs/guides/revert-last-git-commit/#differences-between-reset-and-revert) 4. [Conclusion](https://www.linode.com/docs/guides/revert-last-git-commit/#conclusion) 5. [More Information](https://www.linode.com/docs/guides/revert-last-git-commit/#more-information) - [Cloud Guides & Tutorials]() - [Akamai + Linode]() - [Applications]() - [Audiences]() - [Concentrations]() - [Databases]() - [Development]() - [Install and Use the Deno Javascript Runtime (Node.js Alternative)]() - [Two Scopes of Exception-Handling]() - [Architectures]() - [Awk]() - [Bash]() - [Bug Management and Tracking]() - [C and C++]() - [Continuous Integration]() - [Clojure]() - [Software Architecture Concepts]() - [Data Visualization]() - [Web Application Frameworks]() - [Go]() - [GraphQL]() - [Internet of Things]() - [Java]() - [Javascript]() - [Julia]() - [Next.js]() - [Node.js]() - [Perl]() - [Python]() - [R]() - [React]() - [Ruby on Rails]() - [Rust]() - [Tips and Tricks]() - [Version Control]() - [Backing up Gitlab on Linode Object Storage]() - [Create Git Aliases to Quickly Run Git or Shell Commands]() - [Getting Started with Git]() - [Getting Started with GitHub]() - [How to Install Git and Clone a GitHub Repository]() - [How to Remove a Remote Git]() - [How to Rename a Git Branch]() - [How to Use a GitLab Runner with Machine Driver and Object Storage]() - [Install and Use the Subversion CLI Client]() - [Install Apache Subversion on Ubuntu 20.04]() - [Install GitLab on Ubuntu 18.04]() - [Install GitLab with Docker]() - [Installing Git on Linux, Mac or Windows]() - [Introduction to Version Control]() - [Remove Untracked Files in Git]() - [Resolve Merge Conflicts in Git]() - [Revert the Last Commit in Git]() - [Speed up Your Development Process with Turborepo]() - [SVN vs Git: Which Version Control System Should You Use?]() - [Switching Git Branches]() - [Unbundling NGINX from Omnibus Gitlab]() - [Undo a Git Commit]() - [Use .gitignore to Ignore Specific Files and Folders]() - [Using the Git Rebase Command]() - [See All 30 Version Control Guides](https://www.linode.com/docs/guides/development/version-control/) - [Web Frameworks]() - [Web Assembly]() - [Email Server Guides]() - [Game Servers]() - [Kubernetes]() - [Languages]() - [IPs, Networking & Domains]() - [Linode Platform]() - [Quick Answers]() - [Security, Upgrades & Backups]() - [Tools & Reference]() - [Uptime & Analytics]() - [Web Server Guides]() - [Website Guides]() - [Reference Architectures]() - [Marketplace Docs]() - [Products]() - [API]() - [Blog]() - [Content Resources]() Filtered by search term 1. [Docs Home](https://www.linode.com/docs/) List Sections Tiles Product docs and API reference are now on Akamai TechDocs. [Search product docs. Search for “” in product docs.]() [Search API reference. Search for “” in API reference.]() # Search Results results matching results ## No Results [![Article image]()]() Previous Next ## Filters Filters ( ) #### All #### Add tags All #### Add authors All 1. [Docs Home](https://www.linode.com/docs/) 2. [Guides](https://www.linode.com/docs/guides/) 3. [Development](https://www.linode.com/docs/guides/development/) 4. [Version Control](https://www.linode.com/docs/guides/development/version-control/) # Revert the Last Commit in Git Updated February 15, 2024 Originally authored by [Nathaniel Stickman](https://www.linode.com/docs/contributors/nathaniel-stickman/) - [Report an Issue]() - [View File](https://github.com/linode/docs/blob/develop/docs/guides/development/version-control/revert-last-git-commit/index.md) - [Edit File]() - [Share on Twitter]() - [Share on Facebook]() - [Share on Hacker News]() > **Traducciones al Español** > > Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso. [Create a Linode account]() to try this guide with a \$100 credit. This credit will be applied to any valid services used during your first 60 days. Git is a widely used Version Control System (VCS) known for its versatility. It utilizes local clones of central repositories and keeps track of every commit so changes can be rolled back as needed. You might need that recoverability after an inadvertent commit or to undo a commit for any reason. This tutorial shows you how to use the `git` command line utility to revert a commit. It covers methods using both the `git revert` and `git reset` commands and explains the differences. Learn more about Git generally in our guide [Git vs SVN: Pros and Cons of Each Version Control System](https://www.linode.com/docs/guides/svn-vs-git/#what-is-the-git-version-control-system). For a more general, and thorough, coverage of reverting Git commits, take a look at our guide on [How to Undo a Git Commit](https://www.linode.com/docs/guides/how-to-undo-git-commit/). ## Optional: Create a Test Repository If you’d like to test reverting and resetting in a separate repository from one you actively work in, follow the steps below. This will set up an example Git repository similar to the one used for the examples in this tutorial. The commit IDs may be different, but the contents of the repository should otherwise be the same. The steps presume you have already installed Git and done basic configuration (e.g. user email address and name). If you have not done this yet, you can learn how in our guide [How to Install Git and Clone a GitHub Repository](https://www.linode.com/docs/guides/how-to-install-git-and-clone-a-github-repository/). 1. Create a new directory for your Git repository, and change it into that directory. Here, the new directory, `git-example` is created in the current user’s home directory. ``` mkdir ~/git-example cd ~/git-example ``` From here on, you should execute the given commands while working in this directory. 2. Initialize the new Git repository. ``` git init ``` 3. Use the `touch` command to create some new empty files. ``` touch example-file-1.txt touch example-file-2.txt ``` 4. Add the files to the Git staging area, then commit the staged changes. ``` git add . git commit -m "Initialized repo." ``` 5. Make some changes to the first file, adding some content to it. Then stage and commit those changes using the following commands: ``` echo "Some example text for the first file." >> example-file-1.txt git add example-file-1.txt git commit -m "Added text to first file." ``` 6. Do the same for the second file. ``` echo "Some example text for the second file." >> example-file-2.txt git add example-file-2.txt git commit -m "Added text to second file." ``` 7. You now have a Git repository with a couple of files and several commits, which you can see listed with the following command: ``` git log --oneline ``` The output of the command looks like the following: ``` f4391b2 (HEAD -> master) Added text to second file. e3c534a Added text to first file. 0b24777 Initialized repo. ``` ## How to Use the `git revert` Command on the Last Commit Git revert undoes a commit by comparing the changes made in that commit to the repository’s previous state. It then creates a new commit that reverts the changes. To use the `git revert` command, you first need the ID for that commit. You can get this with the `git log` command. Here, the command is used with the `--oneline` option to make each commit display on a single line: ``` git log --oneline ``` ``` f4391b2 (HEAD -> master) Added text to second file. e3c534a Added text to first file. 0b24777 Initialized repo. ``` The line at the top of the output, with an ID of `f4391b2` in this example, represents the last commit. You can revert that commit using the `revert` command with that commit’s ID, as shown here: ``` git revert f4391b2 ``` Git starts a new commit to revert the changes. It may present you with a text editor allowing you to edit the description for the new commit. When you are satisfied with the description, exit the text editor to complete the reversion. ``` [master e86542a] Revert "Added text to second file." 1 file changed, 1 deletion(-) ``` Running the `git log` command again shows that there is now a new commit to revert the previous commit: ``` git log --oneline ``` ``` e86542a (HEAD -> master) Revert "Added text to second file." f4391b2 Added text to second file. e3c534a Added text to first file. 0b24777 Initialized repo. ``` If you view the affected file, you can see that the changes have been reversed. In this case, that is `example-file-2.txt`: ``` cat example-file-2.txt ``` ## How to Use the `git reset` Command to Undo Commits Git reset can also be used to revert the last commit. The command is more powerful than `git revert` and works by removing commits entirely from the repository’s commit history. Essentially, reset “rewinds” you to a previous commit, eliminating later commits and history along the way. With the `git reset` command, you have access to the `HEAD~1` alias. This alias stands in for the ID of the previous commit, providing easy access when trying to revert to the last commit. The `git reset` command comes with three flags that define how the command deals with changed files in the working directory and staging area. - Use the `--soft` option to roll back to a previous commit, while preserving file changes in the working directory and staging area. ``` git reset --soft HEAD~1 ``` - Use the `--hard` option to likewise roll back to a previous commit. However, this option results in all file changes being reverted as well. Changes to files in the working directory as well as staged changes are both discarded. ``` git reset --hard HEAD~1 ``` - Use the `--mixed` option to, again, roll back to a previous commit. Here, a middle ground is adopted for file changes. As with the `--hard` option, changes made to files in the working directory are discarded. However, like the `--soft` option, staged changes are preserved. ``` git reset --mixed HEAD~1 ``` After any of the above commands, you can see that the last commit has been removed from the Git commit history: ``` git log --oneline ``` ``` e3c534a (HEAD -> master) Added text to first file. 0b24777 Initialized repo. ``` ### Differences Between Reset and Revert The main difference between the `git reset` and `git revert` commands is the commit history. Revert aims to maintain a full commit history, undoing a commit by assessing changes and making a new commit that reverses them. This actually means that this command adds to the commit history. It gives you full transparency to past commits and their reversions. In contrast, using reset discards commits even from the commit history, making them impossible to recover later. The `git reset` command can even do the same for local file changes. This option keeps you from seeing reversions and the original commits that are reverted. Generally, it is recommended that you use `git revert` for backing out a commit. It keeps a record of the removed commit and leaves the possibility of assessing and accessing the commit history later. The `git reset` command should be used sparingly. Take a good look at the situation to ensure that it cannot be remedied by reverting a commit instead of resetting to a previous commit. A reset might be preferred, however, for immediately undoing mistaken commits, when there is less chance that you need options for recovering the changes. ## Conclusion That covers all you need to revert recent Git commits. Moreover, the techniques covered in this tutorial can also help you manage Git commits more generally. The `git revert` command can be useful for precisely removing past commits while retaining your commit history. The `git reset` command, on the other hand, provides a more radical option, completely reverting a repository to a previous commit, including the commit history. To keep learning, refer to the links at the beginning of this guide. These give you more on Git generally as well as more on the commands covered in this tutorial. You may also want to look at our entire lineup of [guides on version control](https://www.linode.com/docs/guides/development/version-control/). These cover everything from the fundamentals to particular use cases, and provide steps to deepen your version control knowledge. ## More Information You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials. - [freeCodeCamp: Git Revert Commit – How to Undo the Last Commit](https://www.freecodecamp.org/news/git-revert-commit-how-to-undo-the-last-commit/) - [TechTarget: How to Revert a Git Commit](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example) This page was originally published on August 18, 2022. - [Report an Issue]() - [View File](https://github.com/linode/docs/blob/develop/docs/guides/development/version-control/revert-last-git-commit/index.md) - [Edit File]() - [Share on Twitter]() - [Share on Facebook]() - [Share on Hacker News]() *** *** Join the conversation. Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our [Support team](https://www.linode.com/support/) or asking on our [Community Site](https://www.linode.com/community/questions/). The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please [update your Cookie Preferences](https://www.linode.com/docs/guides/revert-last-git-commit/) on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser. Please enable JavaScript to view the \<a href=http://disqus.com/?ref\_noscript\>comments powered by Disqus.\</a\> [comments powered by Disqus](http://disqus.com/) ### On this page 1. [Optional: Create a Test Repository](https://www.linode.com/docs/guides/revert-last-git-commit/#optional-create-a-test-repository) 2. [How to Use the git revert Command on the Last Commit](https://www.linode.com/docs/guides/revert-last-git-commit/#how-to-use-the-git-revert-command-on-the-last-commit) 3. [How to Use the git reset Command to Undo Commits](https://www.linode.com/docs/guides/revert-last-git-commit/#how-to-use-the-git-reset-command-to-undo-commits) 1. [Differences Between Reset and Revert](https://www.linode.com/docs/guides/revert-last-git-commit/#differences-between-reset-and-revert) 4. [Conclusion](https://www.linode.com/docs/guides/revert-last-git-commit/#conclusion) 5. [More Information](https://www.linode.com/docs/guides/revert-last-git-commit/#more-information) #### Products - [Products Overview](https://www.linode.com/products/) - [Essential Compute](https://www.linode.com/products/essential-compute/) - [GPU](https://www.linode.com/products/gpu/) - [Accelerated Compute](https://www.linode.com/products/accelerated-compute/) - [Kubernetes](https://www.linode.com/products/kubernetes/) - [App Platform](https://www.linode.com/products/app-platform/) - [Block Storage](https://www.linode.com/products/block-storage/) - [Object Storage](https://www.linode.com/products/object-storage/) - [Backups](https://www.linode.com/products/backups/) - [Cloud Firewall](https://www.linode.com/products/cloud-firewall/) - [DNS Manager](https://www.linode.com/products/dns-manager/) - [NodeBalancers](https://www.linode.com/products/nodebalancers/) - [Private Networking](https://www.linode.com/products/private-networking/) - [Managed Databases](https://www.linode.com/products/databases/) - [Linode Managed](https://www.linode.com/products/managed/) - [Distributions](https://www.linode.com/distributions/) - [Kernels](https://www.linode.com/kernels/) #### Company - [About Akamai](https://www.akamai.com/company) - [History](https://www.akamai.com/company/company-history) - [Leadership](https://www.akamai.com/company/leadership) - [Facts and Figures](https://www.akamai.com/company/facts-figures) - [Awards](https://www.akamai.com/company/our-awards) - [Board of Directors](https://www.akamai.com/company/leadership/board-of-directors) - [Investor Relations](https://www.ir.akamai.com/) - [Corporate Responsibility](https://www.akamai.com/company/corporate-responsibility) - [Ethics](https://www.akamai.com/company/ethics-and-compliance) - [Locations](https://www.akamai.com/company/locations) - [Vulnerability Reporting](https://www.akamai.com/global-services/support/vulnerability-reporting) - [Accessibility Commitment](https://www.linode.com/accessibility/) #### Careers - [Careers](https://www.akamai.com/careers) - [Working at Akamai](https://www.akamai.com/careers/working-at-akamai) - [Students and Recent Grads](https://www.akamai.com/careers/students-and-recent-graduates) - [Workplace Diversity](https://www.akamai.com/careers/workplace-diversity) - [Search Jobs](https://akamaicareers.inflightcloud.com/search?searchable=%5B%5D&section=aka_ext) - [Culture Blog](https://www.akamai.com/blog/culture) #### Newsroom - [Newsroom](https://www.akamai.com/newsroom) - [Press Releases](https://www.akamai.com/newsroom/press-release) - [In the News](https://www.akamai.com/newsroom/in-the-news) - [Media Resources](https://www.akamai.com/newsroom/media-resources) #### Legal & Compliance - [Linode Legal](https://www.linode.com/legal/) - [Akamai Legal](https://www.akamai.com/legal) - [Linode Terms of Service (MSA)](https://www.linode.com/legal-msa/) - [EU Digital Services Act (DSA)](https://www.linode.com/eu-digital-services-act/) - [Information Security Compliance](https://www.akamai.com/legal/compliance) - [Privacy Trust Center](https://www.akamai.com/legal/compliance/privacy-trust-center) - [Cookie Settings](https://www.linode.com/legal/manage-cookie-preferences/) #### Glossary - [What Is Cloud Security?](https://www.akamai.com/glossary/what-is-cloud-security) - [What Is Cloud Redundancy?](https://www.akamai.com/glossary/what-is-cloud-redundancy) - [What Is Cloud Computing Security?](https://www.akamai.com/glossary/what-is-cloud-computing-security) - [What is Cloud Portability?](https://www.akamai.com/glossary/what-is-cloud-portability) - [What Is Event-Driven Architecture?](https://www.akamai.com/glossary/what-is-event-driven-architecture) - [See all](https://www.akamai.com/glossary) [![Akamai Logo](https://www.linode.com/linode/en/images/logo/akamai-logo.svg)](https://www.akamai.com/) - Cookie Preferences - [Support](https://www.linode.com/support/) - [Contact Us](https://www.linode.com/company/contact/) - [System Status](https://status.linode.com/) - [Site Map](https://www.linode.com/site-map/) - © 2003-2025 Linode LLC. All rights reserved.
Readable Markdown
Git is a widely used Version Control System (VCS) known for its versatility. It utilizes local clones of central repositories and keeps track of every commit so changes can be rolled back as needed. You might need that recoverability after an inadvertent commit or to undo a commit for any reason. This tutorial shows you how to use the `git` command line utility to revert a commit. It covers methods using both the `git revert` and `git reset` commands and explains the differences. Learn more about Git generally in our guide [Git vs SVN: Pros and Cons of Each Version Control System](https://www.linode.com/docs/guides/svn-vs-git/#what-is-the-git-version-control-system). For a more general, and thorough, coverage of reverting Git commits, take a look at our guide on [How to Undo a Git Commit](https://www.linode.com/docs/guides/how-to-undo-git-commit/). ## Optional: Create a Test Repository If you’d like to test reverting and resetting in a separate repository from one you actively work in, follow the steps below. This will set up an example Git repository similar to the one used for the examples in this tutorial. The commit IDs may be different, but the contents of the repository should otherwise be the same. The steps presume you have already installed Git and done basic configuration (e.g. user email address and name). If you have not done this yet, you can learn how in our guide [How to Install Git and Clone a GitHub Repository](https://www.linode.com/docs/guides/how-to-install-git-and-clone-a-github-repository/). 1. Create a new directory for your Git repository, and change it into that directory. Here, the new directory, `git-example` is created in the current user’s home directory. ``` mkdir ~/git-example cd ~/git-example ``` From here on, you should execute the given commands while working in this directory. 2. Initialize the new Git repository. ``` git init ``` 3. Use the `touch` command to create some new empty files. ``` touch example-file-1.txt touch example-file-2.txt ``` 4. Add the files to the Git staging area, then commit the staged changes. ``` git add . git commit -m "Initialized repo." ``` 5. Make some changes to the first file, adding some content to it. Then stage and commit those changes using the following commands: 6. Do the same for the second file. 7. You now have a Git repository with a couple of files and several commits, which you can see listed with the following command: ``` git log --oneline ``` The output of the command looks like the following: ``` f4391b2 (HEAD -> master) Added text to second file. e3c534a Added text to first file. 0b24777 Initialized repo. ``` ## How to Use the `git revert` Command on the Last Commit Git revert undoes a commit by comparing the changes made in that commit to the repository’s previous state. It then creates a new commit that reverts the changes. To use the `git revert` command, you first need the ID for that commit. You can get this with the `git log` command. Here, the command is used with the `--oneline` option to make each commit display on a single line: ``` git log --oneline ``` ``` f4391b2 (HEAD -> master) Added text to second file. e3c534a Added text to first file. 0b24777 Initialized repo. ``` The line at the top of the output, with an ID of `f4391b2` in this example, represents the last commit. You can revert that commit using the `revert` command with that commit’s ID, as shown here: ``` git revert f4391b2 ``` Git starts a new commit to revert the changes. It may present you with a text editor allowing you to edit the description for the new commit. When you are satisfied with the description, exit the text editor to complete the reversion. ``` [master e86542a] Revert "Added text to second file." 1 file changed, 1 deletion(-) ``` Running the `git log` command again shows that there is now a new commit to revert the previous commit: ``` git log --oneline ``` ``` e86542a (HEAD -> master) Revert "Added text to second file." f4391b2 Added text to second file. e3c534a Added text to first file. 0b24777 Initialized repo. ``` If you view the affected file, you can see that the changes have been reversed. In this case, that is `example-file-2.txt`: ``` cat example-file-2.txt ``` ## How to Use the `git reset` Command to Undo Commits Git reset can also be used to revert the last commit. The command is more powerful than `git revert` and works by removing commits entirely from the repository’s commit history. Essentially, reset “rewinds” you to a previous commit, eliminating later commits and history along the way. With the `git reset` command, you have access to the `HEAD~1` alias. This alias stands in for the ID of the previous commit, providing easy access when trying to revert to the last commit. The `git reset` command comes with three flags that define how the command deals with changed files in the working directory and staging area. - Use the `--soft` option to roll back to a previous commit, while preserving file changes in the working directory and staging area. ``` git reset --soft HEAD~1 ``` - Use the `--hard` option to likewise roll back to a previous commit. However, this option results in all file changes being reverted as well. Changes to files in the working directory as well as staged changes are both discarded. ``` git reset --hard HEAD~1 ``` - Use the `--mixed` option to, again, roll back to a previous commit. Here, a middle ground is adopted for file changes. As with the `--hard` option, changes made to files in the working directory are discarded. However, like the `--soft` option, staged changes are preserved. ``` git reset --mixed HEAD~1 ``` After any of the above commands, you can see that the last commit has been removed from the Git commit history: ``` git log --oneline ``` ``` e3c534a (HEAD -> master) Added text to first file. 0b24777 Initialized repo. ``` ### Differences Between Reset and Revert The main difference between the `git reset` and `git revert` commands is the commit history. Revert aims to maintain a full commit history, undoing a commit by assessing changes and making a new commit that reverses them. This actually means that this command adds to the commit history. It gives you full transparency to past commits and their reversions. In contrast, using reset discards commits even from the commit history, making them impossible to recover later. The `git reset` command can even do the same for local file changes. This option keeps you from seeing reversions and the original commits that are reverted. Generally, it is recommended that you use `git revert` for backing out a commit. It keeps a record of the removed commit and leaves the possibility of assessing and accessing the commit history later. The `git reset` command should be used sparingly. Take a good look at the situation to ensure that it cannot be remedied by reverting a commit instead of resetting to a previous commit. A reset might be preferred, however, for immediately undoing mistaken commits, when there is less chance that you need options for recovering the changes. ## Conclusion That covers all you need to revert recent Git commits. Moreover, the techniques covered in this tutorial can also help you manage Git commits more generally. The `git revert` command can be useful for precisely removing past commits while retaining your commit history. The `git reset` command, on the other hand, provides a more radical option, completely reverting a repository to a previous commit, including the commit history. To keep learning, refer to the links at the beginning of this guide. These give you more on Git generally as well as more on the commands covered in this tutorial. You may also want to look at our entire lineup of [guides on version control](https://www.linode.com/docs/guides/development/version-control/). These cover everything from the fundamentals to particular use cases, and provide steps to deepen your version control knowledge. ## More Information You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials. - [freeCodeCamp: Git Revert Commit – How to Undo the Last Commit](https://www.freecodecamp.org/news/git-revert-commit-how-to-undo-the-last-commit/) - [TechTarget: How to Revert a Git Commit](https://www.theserverside.com/tutorial/How-to-git-revert-a-commit-A-simple-undo-changes-example) This page was originally published on August 18, 2022.
Shard95 (laksa)
Root Hash1749289081436496295
Unparsed URLcom,linode!www,/docs/guides/revert-last-git-commit/ s443