🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 175 (from laksa154)

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
16 hours ago
🤖
ROBOTS SERVER UNREACHABLE
Failed to connect to robots server: Operation timed out after 2002 milliseconds with 0 bytes received

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0 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.cloudbees.com/blog/git-undo-commit
Last Crawled2026-04-16 17:59:14 (16 hours ago)
First Indexed2020-12-01 16:44:26 (5 years ago)
HTTP Status Code200
Meta TitleHow to Undo Changes in Git with Git Undo Commit
Meta DescriptionNeed to fix a mistake, remove changes, revert to a previous state, or modify a message? Learn various ways to make changes in Git by using undo commit.
Meta Canonicalnull
Boilerpipe Text
Updated on April 20, 2025 In life, undoing our mistakes is something we've always wished was possible. While life might not always present us with a chance to undo our mistakes, Git provides us ample opportunities to do just that. Lucky us! Undoing things in Git isn't just for mistakes. There are many other valid reasons you'd want to travel in time between different versions of a project. For example, maybe you're working on a new feature that isn't ready yet, and then you need to roll back to a previous commit.  However, before we look at the ways you can undo mistakes, let's first reiterate what a Git command is. What is a Git Command? A Git command is an instruction used within Git to manage and track changes within repositories effectively. Git commands allow developers to perform essential version control operations, such as committing updates, reviewing project history, reverting mistakes, and merging changes between branches. These commands typically follow the syntax git <command> and can accept various options or parameters to perform specific tasks. Understanding Git commands is crucial because they empower developers to collaborate seamlessly, maintain organized project histories, and efficiently manage code changes. Whether it's committing new work, reverting to a previous version, or resolving conflicts in shared repositories, mastering Git commands ensures that developers can maintain robust, accurate, and flexible project histories. In this post, we'll explore ways to undo changes in Git. So, next time you think you've made a commit in error, you'll have no reason at all to panic. As you know, Git stores snapshots of a repo at different points and then creates a timeline history. We'll be taking advantage of this timeline to move back and forth as we please. Let's jump right into it! To make this post practical and relatable, I'll be creating a new git folder to use as a demo. I advise you to do the same so you can easily follow along. We'll be doing everything from the terminal, so get ready to do some typing.  Undoing Changes With Git From my terminal, I'll run the following commands to create the repo folder, add some files, and create some commits to work with:  mkdir git-undo && cd git-undo git init touch page1.txt && echo "Let us put something here" > page1.txt git add page.txt git commit -m "create page1" Now, we have a repo set up with one file added and one commit in the git history. Let's add a few more files with separate commits so we can have more commits to work with. touch page2.txt && echo "Content for page2" > page2.txt git add page2.txt git commit -m "create page2" touch page3.txt && echo "Content for page3" > page3.txt git add page3.txt git commit -m "create page3" Checking Git History To be able to travel back and forth in time, we need a way to know where we are. We also need a list of possible places and times we can travel to. And that's where the Git history comes in handy. There are two major ways to view the git history of a repo: git reflog and  git log . While these two commands serve a similar purpose (viewing history), there are subtle differences in how they work and the results they show. Either of the two will serve us just fine, however, for the scope of this tutorial. So let's use git reflog. To read more about their differences, check out this StackOverflow answer. When you run the git reflog command, you should have something like this: Git reflog shows a list of all commits that have been made in this repo, as well as some other key events, like when switching between branches ( checkout ). Each commit is identified by a SHA-1 hash (before the commit message). You can use these SHA-1 IDs to travel to any commit whenever you want.  Note: Navigating between commits improperly can sometimes result in a detached HEAD state —make sure you know what this means to avoid confusion. Undoing Uncommitted Changes The first approach we're going to look at in undoing changes is how to undo changes you've made but not yet committed. Whether you've staged these changes or not, what matters is that you haven't committed them. Let's make some changes to the third file we created earlier (page3.txt).  echo "making some changes to page3" > page3.txt After running the above command, run  git status . You should see something like this: Let's assume you made the above change in error. Luckily, you realized the problem before making the commit. Now, you want to restore the repo to how it was before you made the change. There are three ways to go about it:  git stash :  The git stash command will discard all your untracked files, as well as staged and unstaged modifications. However, Git will temporarily save them, in case you want to retrieve them later. git checkout --<file> : This works similarly to git stash , except that it discards changes to a file permanently. git reset --hard : This also discards all changes permanently. Which option is best? I mostly use git stash because I can reapply the discarded changes later. When I'm absolutely sure I won't ever need those changes, I use one of the other options. Additionally, if there are untracked files cluttering the workspace that I’m certain I don’t need, I’ll use git clean to remove them . This helps maintain a tidy and organized repository while ensuring no unnecessary files linger. Undoing Committed Changes (Git Reset) Now, let's assume you didn't realize you made an error before you committed. Fret not! You can easily go back to a previous commit from where you're sure there's no error. Let's create a new page and commit it to see how to do this.  echo "some new content for the page 4" > page4.txt git add page4.txt git commit -m "create page 4" We want to restore our repo to when we had just three pages. Here's how to do that:  Run git reflog to see a history of all commits made. Then note down the ID (SHA-1 hash) of the commit we want to go back to. Now run git reset --hard <commit id> . As you can see in the image above, the commit hash when we had 3 pages was 6b6271a . We then passed this hash to the git reset --hard  command to restore the repo to that commit.  Undoing Your Last Commit What if, after committing a series of changes, you make some changes that really should have been a part of the last commit? There's a way to undo—or, more accurately, amend—the most recent commit. We can amend the last commit by running the git commit --amend command. Running this command will open your configured text editor so that you can edit the last commit message. Before running git command --amend , remember to first stage your new changes with git add . Close your text editor after typing in the new commit message. When you come back to your terminal, you'll see something like this: A Note on Shared Repos The three undo methods we've looked at above are best applied to local repos you're working on alone when you haven't pushed your commits to a remote repo. A shared repo introduces a different dynamic to how you undo changes. Beyond Git, it's important that you communicate with other members sharing the repo so they're all aware and on the same page as you. One thing to consider when working on a shared repo is to use git branches for your local work. That way, your work will not interfere with the main branch. You merge into the main shared branch only when you're sure your work contains no errors. That said, what if, for any reason, you have to do a git reset on a shared branch and you try to git push? Git will refuse the push, complaining that your local copy is out of date and missing some commits.  A brute force solution to this problem is to use the  git push --force command. This command pushes your local copy to the remote repo and overwrites the history of the remote repo to be in sync with yours. However, this creates problems for other people who are sharing the repo. Also, if someone else also uses the git push --force command, it overwrites what you've done. That's why communication with team members is important. Git push --force is something I always avoid. I advise you to avoid it, too, unless it's absolutely necessary.  Undoing Changes in a Shared Repo If you must undo changes in a shared branch of a shared repo, the best tool to use is git revert <commit id> . It reverts the changes done by the commit you specified, and then it creates a new commit for it. In other words, the git revert inverts the changes of a specified commit as a new commit. This way, the history of the repo keeps going forward, and other collaborators don't pull a messed-up repo.  Recap of All Commands Let's do a quick summary, since we've covered a lot in this post.  Use Git reflog to check commits history. Git stash lets you discard changes and save them for later reuse. Try Git checkout --<file> to discard uncommitted changes to a file. Git reset --hard is for when you want to discard all uncommitted changes. Use Git reset --hard <commit id> to point the repo to a previous commit. Now, you've seen the various methods Git provides for undoing changes. The next time you need to time travel in your project and undo things, always remember that Git has you covered. Check out the CloudBees blog to learn more about Git, GitOps , Git Switch Branch , Advanced Git with Jenkins , Git Push , Git Pull , and much more. 
Markdown
[![CloudBees Logo](https://www.cloudbees.com/wf/CloudBees.svg)](https://www.cloudbees.com/) [![CloudBees Logo](https://www.cloudbees.com/wf/CloudBees.svg)](https://www.cloudbees.com/) [Book a Demo](https://www.cloudbees.com/contact-us) - Product [![](https://www.cloudbees.com/wf/Unify-Overview-nav-feature.avif)CloudBees Unify Overview Explore](https://www.cloudbees.com/unify) - [![](https://www.cloudbees.com/wf/workflow.svg)CI/CD Streamline CI/CD workflows for faster innovation and reliable deployments](https://www.cloudbees.com/capabilities/ci-cd-workflows) - [![](https://www.cloudbees.com/wf/shield-check.svg)Security and Compliance Continuous security and compliance assessment with control insights](https://www.cloudbees.com/capabilities/compliance) - [![](https://www.cloudbees.com/wf/flag.svg)Feature Management Control feature rollouts with flags for safer, faster releases and testing](https://www.cloudbees.com/capabilities/feature-management) - [![](https://www.cloudbees.com/wf/flask-conical.svg)Smart Tests AI-driven test intelligence that delivers developer feedback 80% faster](https://www.cloudbees.com/capabilities/cloudbees-smart-tests) - [![](https://www.cloudbees.com/wf/waypoints.svg)Release Orchestration Orchestrate complex releases with DevOps ecosystem visibility](https://www.cloudbees.com/capabilities/release-orchestration) - [![](https://www.cloudbees.com/wf/chart-column.svg)Analytics Single source of truth for software delivery insights and metrics](https://www.cloudbees.com/capabilities/analytics) - Solutions Use Cases - [![](https://www.cloudbees.com/wf/server-cog.svg)Jenkins Modernization and Migration](https://www.cloudbees.com/use-case/jenkins-modernization) - [![](https://www.cloudbees.com/wf/bot.svg)Agentic DevOps](https://www.cloudbees.com/use-case/agentic-devops) - [![](https://www.cloudbees.com/wf/network.svg)Multi-Tool Orchestration and Governance](https://www.cloudbees.com/use-case/orchestration-and-governance) - [![](https://www.cloudbees.com/wf/brick-wall-shield.svg)Continuous Security, Compliance and AI Guardrails](https://www.cloudbees.com/use-case/security-and-compliance) Industries - [![](https://www.cloudbees.com/wf/car.svg)Automotive](https://www.cloudbees.com/industries/automotive) - [![](https://www.cloudbees.com/wf/landmark.svg)Government](https://www.cloudbees.com/industries/government) - [![](https://www.cloudbees.com/wf/banknote.svg)Financial Services](https://www.cloudbees.com/industries/financial-services) - [![](https://www.cloudbees.com/wf/umbrella.svg)Insurance](https://www.cloudbees.com/industries/insurance) - [![](https://www.cloudbees.com/wf/store.svg)Retail](https://www.cloudbees.com/industries/retail) - [![](https://www.cloudbees.com/wf/code.svg)Software](https://www.cloudbees.com/industries/software) Audiences - [![](https://www.cloudbees.com/wf/user.svg)Executive](https://www.cloudbees.com/ci-for-executives) - Resources Discover - [![](https://www.cloudbees.com/wf/badge-plus.svg)AI Design Partner Program](https://ai.cloudbees.com/) - [![](https://www.cloudbees.com/wf/layout-dashboard.svg)Resources Overview Page](https://www.cloudbees.com/resources) - [![](https://www.cloudbees.com/wf/newspaper.svg)Blog](https://www.cloudbees.com/blog) - [![](https://www.cloudbees.com/wf/video.svg)Webinars & Videos](https://www.cloudbees.com/videos) - [![](https://www.cloudbees.com/wf/calendar-days.svg)Events](https://www.cloudbees.com/events) - [![](https://www.cloudbees.com/wf/book-open.svg)Guides & Reports](https://www.cloudbees.com/whitepapers) - [![](https://www.cloudbees.com/wf/message-square-quote.svg)Customer Stories](https://www.cloudbees.com/customer-stories) Security - [![](https://www.cloudbees.com/wf/check-badge.svg)Trust Center](https://www.cloudbees.com/company/trust-center) - [![](https://www.cloudbees.com/wf/bell.svg)Security Advisories](https://www.cloudbees.com/security-advisories) Get help - [![](https://www.cloudbees.com/wf/book-marked.svg)Documentation](https://docs.cloudbees.com/) - [![](https://www.cloudbees.com/wf/life-buoy.svg)Support](https://support.cloudbees.com/hc/en-us) - [![](https://www.cloudbees.com/wf/user-check.svg)Customer Success & Professional Services](https://www.cloudbees.com/services) - About Company - [![](https://www.cloudbees.com/wf/info.svg)About Us](https://www.cloudbees.com/about-us) - [![](https://www.cloudbees.com/wf/megaphone.svg)Press and Recognition](https://www.cloudbees.com/newsroom) - [![](https://www.cloudbees.com/wf/handshake.svg)Partners](https://www.cloudbees.com/partners) - [![](https://www.cloudbees.com/wf/briefcase-business.svg)Careers](https://www.cloudbees.com/careers) Compare CloudBees - [vs GitHub Actions](https://www.cloudbees.com/blog/cloudbees-vs-github-actions-choosing-a-ci-cd-platform-for-your-business) - [vs Jenkins Open Source](https://www.cloudbees.com/blog/cloudbees-ci-enhancing-jenkins-for-business-and-mission-critical) Featured article [Introducing the CloudBees Unify AI Assistant: Context-Aware AI for the Modern SDLC Explore](https://www.cloudbees.com/blog/introducing-the-cloudbees-unify-ai-assistant-context-aware-ai-for-the-modern) [Book a demo](https://www.cloudbees.com/contact-us) How-to's and Support # How to Undo Changes in Git with Git Undo Commit ![](https://images.ctfassets.net/vtn4rfaw6n2j/5D7f8OhtdZC6x8MnxGTiV5/762b41e2a25522968d49208efd6d1abb/javier-allegue-barros-C7B-ExXpOIE-unsplash.jpg?w=640&h=424) ![](https://images.ctfassets.net/vtn4rfaw6n2j/3a2Z4nQQkU87VPt7Q65tEH/6a2e3c6ffc1416256411d86048589947/Bee-Georgie.jpg?w=96&h=96&fit=thumb&f=face) Written by: Georgiana Patru 9 min read Subscribe *Updated on April 20, 2025* In life, undoing our mistakes is something we've always wished was possible. While life might not always present us with a chance to undo our mistakes, [Git](https://en.wikipedia.org/wiki/Git) provides us ample opportunities to do just that. Lucky us! Undoing things in Git isn't just for mistakes. There are many other valid reasons you'd want to travel in time between different versions of a project. For example, maybe you're working on a new feature that isn't ready yet, and then you need to roll back to a previous commit. However, before we look at the ways you can undo mistakes, let's first reiterate what a Git command is. [What is a Git Command?](https://www.cloudbees.com/blog/git-undo-commit#what-is-a-git-command) A Git command is an instruction used within Git to manage and track changes within repositories effectively. Git commands allow developers to perform essential version control operations, such as committing updates, reviewing project history, reverting mistakes, and merging changes between branches. These commands typically follow the syntax `git <command>` and can accept various options or parameters to perform specific tasks. Understanding Git commands is crucial because they empower developers to collaborate seamlessly, maintain organized project histories, and efficiently manage code changes. Whether it's committing new work, reverting to a previous version, or resolving conflicts in shared repositories, mastering Git commands ensures that developers can maintain robust, accurate, and flexible project histories. In this post, we'll explore ways to undo changes in Git. So, next time you think you've made a commit in error, you'll have no reason at all to panic. As you know, Git stores snapshots of a repo at different points and then creates a timeline history. We'll be taking advantage of this timeline to move back and forth as we please. Let's jump right into it\! To make this post practical and relatable, I'll be creating a new git folder to use as a demo. I advise you to do the same so you can easily follow along. We'll be doing everything from the terminal, so get ready to do some typing. ## **Undoing Changes With Git** From my terminal, I'll run the following commands to create the repo folder, add some files, and create some commits to work with: Now, we have a repo set up with one file added and one commit in the git history. Let's add a few more files with separate commits so we can have more commits to work with. ### **Checking Git History** To be able to travel back and forth in time, we need a way to know where we are. We also need a list of possible places and times we can travel to. And that's where the Git history comes in handy. There are two major ways to view the git history of a repo: **git reflog** and **git log**. While these two commands serve a similar purpose (viewing history), there are subtle differences in how they work and the results they show. Either of the two will serve us just fine, however, for the scope of this tutorial. So let's use **git reflog.** To read more about their differences, check out [this StackOverflow answer.](https://stackoverflow.com/a/17860179/8278524) When you run the **git reflog** command, you should have something like this: ![git reflog command](https://images.ctfassets.net/vtn4rfaw6n2j/2if8VIgNWm2u0gtgCsbO2/6490df5860d8fff6dfdfd6a18315a4cb/image3.png) **Git reflog** shows a list of all commits that have been made in this repo, as well as some other key events, like when switching between branches (**checkout**). Each commit is identified by a SHA-1 hash (before the commit message). You can use these SHA-1 IDs to travel to any commit whenever you want. *Note: Navigating between commits improperly can sometimes result in a* [***detached HEAD state***](https://www.cloudbees.com/blog/git-detached-head)*—make sure you know what this means to avoid confusion.* [![Unleash the Power of Open Architecture with CloudBees Platform](https://images.ctfassets.net/vtn4rfaw6n2j/2zNg6MndYCobn3VLWYvbXs/5885921ef0423934e79d6272e330b0d3/SaaS-TryforFree-1200x628-Option4.jpg?fit=pad)](https://cloudbees.io/) ### **Undoing Uncommitted Changes** The first approach we're going to look at in undoing changes is how to undo changes you've made but not yet committed. Whether you've staged these changes or not, what matters is that you haven't committed them. Let's make some changes to the third file we created earlier (page3.txt). `echo "making some changes to page3" > page3.txt` After running the above command, run **git status**. You should see something like this: ![git status](https://images.ctfassets.net/vtn4rfaw6n2j/3F1eRiogLlKkL0v3XUFQ7b/39b71540b688c12fcf37587ca434b162/image5.png) Let's assume you made the above change in error. Luckily, you realized the problem before making the commit. Now, you want to restore the repo to how it was before you made the change. There are three ways to go about it: 1. `git stash`**:** The **git stash** command will discard all your untracked files, as well as staged and unstaged modifications. However, Git will temporarily save them, in case you want to retrieve them later. 2. `git checkout --<file>`**:** This works similarly to [git stash](https://www.cloudbees.com/blog/git-stash-a-detailed-guide-to-shelving-your-code), except that it discards changes to a file permanently. 3. `git reset --hard`**:** This also discards all changes permanently. Which option is best? I mostly use **git stash** because I can reapply the discarded changes later. When I'm absolutely sure I won't ever need those changes, I use one of the other options. Additionally, if there are untracked files cluttering the workspace that I’m certain I don’t need, I’ll use [**git clean to remove them**](https://www.cloudbees.com/blog/git-remove-untracked-files). This helps maintain a tidy and organized repository while ensuring no unnecessary files linger. ### **Undoing Committed Changes (Git Reset)** Now, let's assume you didn't realize you made an error before you committed. Fret not! You can easily go back to a previous commit from where you're sure there's no error. Let's create a new page and commit it to see how to do this. We want to restore our repo to when we had just three pages. Here's how to do that: 1. Run **git reflog** to see a history of all commits made. 2. Then note down the ID (SHA-1 hash) of the commit we want to go back to. 3. Now run **git reset --hard \<commit id\>**. ![git reset ](https://images.ctfassets.net/vtn4rfaw6n2j/1BmwzjfrE5m1N8fuh3DDMf/269a3b3b83e04c69c5d94aa4e205568a/image4.png) As you can see in the image above, the commit hash when we had 3 pages was **6b6271a**. We then passed this hash to the **git reset --hard** command to restore the repo to that commit. ### **Undoing Your Last Commit** What if, after committing a series of changes, you make some changes that really should have been a part of the last commit? There's a way to undo—or, more accurately, amend—the most recent commit. We can amend the last commit by running the **git commit --amend** command. Running this command will open your configured text editor so that you can edit the last commit message. Before running **git command --amend**, remember to first stage your new changes with **git add**. Close your text editor after typing in the new commit message. When you come back to your terminal, you'll see something like this: ![Undoing your last commit](https://images.ctfassets.net/vtn4rfaw6n2j/3QHlQ0H5tXBqpbMmxxW5HP/f96e219ef8a85760cb78aa10b8d247d9/image6.png) ### **A Note on Shared Repos** The three undo methods we've looked at above are best applied to local repos you're working on alone when you haven't pushed your commits to a remote repo. A shared repo introduces a different dynamic to how you undo changes. Beyond Git, it's important that you communicate with other members sharing the repo so they're all aware and on the same page as you. One thing to consider when working on a shared repo is to use git branches for your local work. That way, your work will not interfere with the main branch. You merge into the main shared branch only when you're sure your work contains no errors. That said, what if, for any reason, you have to do a git reset on a shared branch and you try to git push? Git will refuse the push, complaining that your local copy is out of date and missing some commits. A brute force solution to this problem is to use the **git push --force** command. This command pushes your local copy to the remote repo and overwrites the history of the remote repo to be in sync with yours. However, this creates problems for other people who are sharing the repo. Also, if someone else also uses the git push --force command, it overwrites what you've done. That's why communication with team members is important. **Git push --force** is something I always avoid. I advise you to avoid it, too, unless it's absolutely necessary. ### **Undoing Changes in a Shared Repo** If you must undo changes in a shared branch of a shared repo, the best tool to use is **git revert \<commit id\>**. It reverts the changes done by the commit you specified, and then it creates a new commit for it. In other words, the [git revert](https://www.cloudbees.com/blog/git-revert-commit) inverts the changes of a specified commit as a new commit. This way, the history of the repo keeps going forward, and other collaborators don't pull a messed-up repo. ## **Recap of All Commands** Let's do a quick summary, since we've covered a lot in this post. - Use`Git reflog` to check commits history. - `Git stash` lets you discard changes and save them for later reuse. - Try `Git checkout --<file>` to discard uncommitted changes to a file. - `Git reset --hard` is for when you want to discard all uncommitted changes. - Use `Git reset --hard <commit id>` to point the repo to a previous commit. Now, you've seen the various methods Git provides for undoing changes. The next time you need to time travel in your project and undo things, always remember that Git has you covered. Check out the [CloudBees blog](https://www.cloudbees.com/blog) to learn more about Git, [GitOps](https://www.cloudbees.com/blog/gitops-dev-dash-ops), [Git Switch Branch](https://www.cloudbees.com/blog/git-switch-branch-everything-to-know), [Advanced Git with Jenkins](https://www.cloudbees.com/blog/advanced-git-jenkins), [Git Push](https://www.cloudbees.com/blog/git-push-an-in-depth-tutorial-with-examples), [Git Pull](https://www.cloudbees.com/blog/git-pull-how-it-works-with-detailed-examples), and much more. Previous [All Blogs](https://www.cloudbees.com/blog/listing) [All Blogs](https://www.cloudbees.com/blog/listing) [Next](https://www.cloudbees.com/blog/software-delivery-velocity) #### Related posts [![Blog-thumb-webinar2-Driving-Velocity-Without-Fragility](https://images.ctfassets.net/vtn4rfaw6n2j/73yggyFMqAIRwzVYJoELGZ/8eed9a306847b703516349545958f909/Blog-thumb-webinar2-Driving-Velocity-Without-Fragility.jpg?w=800&h=540&fit=pad) Driving Velocity Without Fragility: How to Conquer the Hidden Costs of Software Delivery October 1, 2025](https://www.cloudbees.com/blog/software-delivery-velocity) [![Blog-thumb-supercharge-test-impact-analysis-with-CB-smart-tests](https://images.ctfassets.net/vtn4rfaw6n2j/QvDhFJHMnlW53qA4tgvme/0fc42982f8f42c5d86b8b509886b3d30/Blog-thumb-supercharge-test-impact-analysis-with-CB-smart-tests.jpg?w=800&h=540&fit=pad) Test Impact Analysis September 22, 2025](https://www.cloudbees.com/blog/test-impact-analysis) [![Blog-thumb-smarter-testing-with-static-code-analysis](https://images.ctfassets.net/vtn4rfaw6n2j/1YhUnRYa1pCAa6JD8r9gVz/348d950c7d46378a5f854f5e0a38cbd0/Blog-thumb-smarter-testing-with-static-code-analysis.jpg?w=800&h=540&fit=pad) Static Code Analysis Approaches for Handling Code Quality September 22, 2025](https://www.cloudbees.com/blog/static-code-analysis) [![Blog-thumb-machine-learning-transforming-software-testing](https://images.ctfassets.net/vtn4rfaw6n2j/5DycKkYHPIF6L0fwZLYgn8/17cd530fc2524b7c9c0aea44ca4e850e/Blog-thumb-machine-learning-transforming-software-testing.jpg?w=800&h=540&fit=pad) Machine Learning in Software Testing for Faster, Quality Releases September 22, 2025](https://www.cloudbees.com/blog/machine-learning-in-software-testing) [![Blog-thumb-webinar1-CICD-at-Scale](https://images.ctfassets.net/vtn4rfaw6n2j/5R6PCZhS7O7A3EDsFZHlRn/6a543fdd360e3668767beb1cd6e88603/Blog-thumb-webinar1-CICD-at-Scale.jpg?w=800&h=540&fit=pad) Mastering Jenkins at Scale: Enterprise DevOps Webinar Takeaways Unpacked September 18, 2025](https://www.cloudbees.com/blog/jenkins-at-scale-enterprise-devops-webinar-takeaways) [![The Smoke, Sanity, and Regression Testing Triad](https://images.ctfassets.net/vtn4rfaw6n2j/4EGxYESdB5WDKdJoSO3JON/509a18e4764ce52956dbd3c8543c7f85/Blog-thumb-smoke-triad.jpg?w=800&h=540&fit=pad) The Smoke, Sanity, and Regression Testing Triad July 23, 2025](https://www.cloudbees.com/blog/the-smoke-sanity-and-regression-testing-triad) Stay up-to-date with the latest insights Sign up today for the CloudBees newsletter and get our latest and greatest how-to’s and developer insights, product updates and company news\! Signup today [![](https://www.cloudbees.com/wf/CloudBees.svg)](https://www.cloudbees.com/) Product - [CloudBees Unify](https://www.cloudbees.com/unify) - [CI/CD](https://www.cloudbees.com/capabilities/ci-cd-workflows) - [Security and Compliance](https://www.cloudbees.com/capabilities/compliance) - [Feature Management](https://www.cloudbees.com/capabilities/feature-management) - [Smart Tests](https://www.cloudbees.com/capabilities/cloudbees-smart-tests) - [Release Orchestration](https://www.cloudbees.com/capabilities/release-orchestration) - [Analytics](https://www.cloudbees.com/capabilities/analytics) Solutions - [Jenkins Modernization and Migration](https://www.cloudbees.com/use-case/jenkins-modernization) - [Agentic DevOps](https://www.cloudbees.com/use-case/agentic-devops) - [Multi-Tool Orchestration and Governance](https://www.cloudbees.com/use-case/orchestration-and-governance) - [Continuous Security, Compliance and AI Guardrails](https://www.cloudbees.com/use-case/security-and-compliance) Resources - [Blog](https://www.cloudbees.com/blog) - [AI Design Partner Program](https://ai.cloudbees.com/) - [Trust Center](https://www.cloudbees.com/company/trust-center) - [Security Advisories](https://www.cloudbees.com/security-advisories) - [Documentation](https://docs.cloudbees.com/) - [Support](https://support.cloudbees.com/hc/en-us) - [Customer Success and Professional Services](https://www.cloudbees.com/services) - [CI Waste Calculator](https://www.cloudbees.com/ci-waste-calculator) Company - [About Us](https://www.cloudbees.com/about-us) - [Press and Recognition](https://www.cloudbees.com/newsroom) - [Partners](https://www.cloudbees.com/partners) - [Careers](https://www.cloudbees.com/careers) - [Pricing](https://www.cloudbees.com/pricing) © 2026 CloudBees, Inc., CloudBees® and the Infinity logo® are registered trademarks of CloudBees, Inc. in the United States and may be registered in other countries. Other products or brand names may be trademarks or registered trademarks of CloudBees, Inc. or their respective holders. [Terms of Service](https://www.cloudbees.com/company/terms-of-service) ✓ Thanks for sharing\! [AddToAny](https://www.addtoany.com/ "Share Buttons") [More…](https://www.cloudbees.com/blog/git-undo-commit#addtoany "Show all")
Readable Markdown
*Updated on April 20, 2025* In life, undoing our mistakes is something we've always wished was possible. While life might not always present us with a chance to undo our mistakes, [Git](https://en.wikipedia.org/wiki/Git) provides us ample opportunities to do just that. Lucky us! Undoing things in Git isn't just for mistakes. There are many other valid reasons you'd want to travel in time between different versions of a project. For example, maybe you're working on a new feature that isn't ready yet, and then you need to roll back to a previous commit. However, before we look at the ways you can undo mistakes, let's first reiterate what a Git command is. [What is a Git Command?](https://www.cloudbees.com/blog/git-undo-commit#what-is-a-git-command) A Git command is an instruction used within Git to manage and track changes within repositories effectively. Git commands allow developers to perform essential version control operations, such as committing updates, reviewing project history, reverting mistakes, and merging changes between branches. These commands typically follow the syntax `git <command>` and can accept various options or parameters to perform specific tasks. Understanding Git commands is crucial because they empower developers to collaborate seamlessly, maintain organized project histories, and efficiently manage code changes. Whether it's committing new work, reverting to a previous version, or resolving conflicts in shared repositories, mastering Git commands ensures that developers can maintain robust, accurate, and flexible project histories. In this post, we'll explore ways to undo changes in Git. So, next time you think you've made a commit in error, you'll have no reason at all to panic. As you know, Git stores snapshots of a repo at different points and then creates a timeline history. We'll be taking advantage of this timeline to move back and forth as we please. Let's jump right into it\! To make this post practical and relatable, I'll be creating a new git folder to use as a demo. I advise you to do the same so you can easily follow along. We'll be doing everything from the terminal, so get ready to do some typing. ## **Undoing Changes With Git** From my terminal, I'll run the following commands to create the repo folder, add some files, and create some commits to work with: Now, we have a repo set up with one file added and one commit in the git history. Let's add a few more files with separate commits so we can have more commits to work with. ### **Checking Git History** To be able to travel back and forth in time, we need a way to know where we are. We also need a list of possible places and times we can travel to. And that's where the Git history comes in handy. There are two major ways to view the git history of a repo: **git reflog** and **git log**. While these two commands serve a similar purpose (viewing history), there are subtle differences in how they work and the results they show. Either of the two will serve us just fine, however, for the scope of this tutorial. So let's use **git reflog.** To read more about their differences, check out [this StackOverflow answer.](https://stackoverflow.com/a/17860179/8278524) When you run the **git reflog** command, you should have something like this: ![git reflog command](https://images.ctfassets.net/vtn4rfaw6n2j/2if8VIgNWm2u0gtgCsbO2/6490df5860d8fff6dfdfd6a18315a4cb/image3.png) **Git reflog** shows a list of all commits that have been made in this repo, as well as some other key events, like when switching between branches (**checkout**). Each commit is identified by a SHA-1 hash (before the commit message). You can use these SHA-1 IDs to travel to any commit whenever you want. *Note: Navigating between commits improperly can sometimes result in a* [***detached HEAD state***](https://www.cloudbees.com/blog/git-detached-head)*—make sure you know what this means to avoid confusion.* [![Unleash the Power of Open Architecture with CloudBees Platform](https://images.ctfassets.net/vtn4rfaw6n2j/2zNg6MndYCobn3VLWYvbXs/5885921ef0423934e79d6272e330b0d3/SaaS-TryforFree-1200x628-Option4.jpg?fit=pad)](https://cloudbees.io/) ### **Undoing Uncommitted Changes** The first approach we're going to look at in undoing changes is how to undo changes you've made but not yet committed. Whether you've staged these changes or not, what matters is that you haven't committed them. Let's make some changes to the third file we created earlier (page3.txt). `echo "making some changes to page3" > page3.txt` After running the above command, run **git status**. You should see something like this: ![git status](https://images.ctfassets.net/vtn4rfaw6n2j/3F1eRiogLlKkL0v3XUFQ7b/39b71540b688c12fcf37587ca434b162/image5.png) Let's assume you made the above change in error. Luckily, you realized the problem before making the commit. Now, you want to restore the repo to how it was before you made the change. There are three ways to go about it: 1. `git stash`**:** The **git stash** command will discard all your untracked files, as well as staged and unstaged modifications. However, Git will temporarily save them, in case you want to retrieve them later. 2. `git checkout --<file>`**:** This works similarly to [git stash](https://www.cloudbees.com/blog/git-stash-a-detailed-guide-to-shelving-your-code), except that it discards changes to a file permanently. 3. `git reset --hard`**:** This also discards all changes permanently. Which option is best? I mostly use **git stash** because I can reapply the discarded changes later. When I'm absolutely sure I won't ever need those changes, I use one of the other options. Additionally, if there are untracked files cluttering the workspace that I’m certain I don’t need, I’ll use [**git clean to remove them**](https://www.cloudbees.com/blog/git-remove-untracked-files). This helps maintain a tidy and organized repository while ensuring no unnecessary files linger. ### **Undoing Committed Changes (Git Reset)** Now, let's assume you didn't realize you made an error before you committed. Fret not! You can easily go back to a previous commit from where you're sure there's no error. Let's create a new page and commit it to see how to do this. We want to restore our repo to when we had just three pages. Here's how to do that: 1. Run **git reflog** to see a history of all commits made. 2. Then note down the ID (SHA-1 hash) of the commit we want to go back to. 3. Now run **git reset --hard \<commit id\>**. ![git reset ](https://images.ctfassets.net/vtn4rfaw6n2j/1BmwzjfrE5m1N8fuh3DDMf/269a3b3b83e04c69c5d94aa4e205568a/image4.png) As you can see in the image above, the commit hash when we had 3 pages was **6b6271a**. We then passed this hash to the **git reset --hard** command to restore the repo to that commit. ### **Undoing Your Last Commit** What if, after committing a series of changes, you make some changes that really should have been a part of the last commit? There's a way to undo—or, more accurately, amend—the most recent commit. We can amend the last commit by running the **git commit --amend** command. Running this command will open your configured text editor so that you can edit the last commit message. Before running **git command --amend**, remember to first stage your new changes with **git add**. Close your text editor after typing in the new commit message. When you come back to your terminal, you'll see something like this: ![Undoing your last commit](https://images.ctfassets.net/vtn4rfaw6n2j/3QHlQ0H5tXBqpbMmxxW5HP/f96e219ef8a85760cb78aa10b8d247d9/image6.png) ### **A Note on Shared Repos** The three undo methods we've looked at above are best applied to local repos you're working on alone when you haven't pushed your commits to a remote repo. A shared repo introduces a different dynamic to how you undo changes. Beyond Git, it's important that you communicate with other members sharing the repo so they're all aware and on the same page as you. One thing to consider when working on a shared repo is to use git branches for your local work. That way, your work will not interfere with the main branch. You merge into the main shared branch only when you're sure your work contains no errors. That said, what if, for any reason, you have to do a git reset on a shared branch and you try to git push? Git will refuse the push, complaining that your local copy is out of date and missing some commits. A brute force solution to this problem is to use the **git push --force** command. This command pushes your local copy to the remote repo and overwrites the history of the remote repo to be in sync with yours. However, this creates problems for other people who are sharing the repo. Also, if someone else also uses the git push --force command, it overwrites what you've done. That's why communication with team members is important. **Git push --force** is something I always avoid. I advise you to avoid it, too, unless it's absolutely necessary. ### **Undoing Changes in a Shared Repo** If you must undo changes in a shared branch of a shared repo, the best tool to use is **git revert \<commit id\>**. It reverts the changes done by the commit you specified, and then it creates a new commit for it. In other words, the [git revert](https://www.cloudbees.com/blog/git-revert-commit) inverts the changes of a specified commit as a new commit. This way, the history of the repo keeps going forward, and other collaborators don't pull a messed-up repo. ## **Recap of All Commands** Let's do a quick summary, since we've covered a lot in this post. - Use`Git reflog` to check commits history. - `Git stash` lets you discard changes and save them for later reuse. - Try `Git checkout --<file>` to discard uncommitted changes to a file. - `Git reset --hard` is for when you want to discard all uncommitted changes. - Use `Git reset --hard <commit id>` to point the repo to a previous commit. Now, you've seen the various methods Git provides for undoing changes. The next time you need to time travel in your project and undo things, always remember that Git has you covered. Check out the [CloudBees blog](https://www.cloudbees.com/blog) to learn more about Git, [GitOps](https://www.cloudbees.com/blog/gitops-dev-dash-ops), [Git Switch Branch](https://www.cloudbees.com/blog/git-switch-branch-everything-to-know), [Advanced Git with Jenkins](https://www.cloudbees.com/blog/advanced-git-jenkins), [Git Push](https://www.cloudbees.com/blog/git-push-an-in-depth-tutorial-with-examples), [Git Pull](https://www.cloudbees.com/blog/git-pull-how-it-works-with-detailed-examples), and much more.
Shard175 (laksa)
Root Hash9451514753958949575
Unparsed URLcom,cloudbees!www,/blog/git-undo-commit s443