Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
How to Undo “git commit –amend"?
Next article icon

How to Undo “git commit –amend"?

Last Updated : 27 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Git is a powerful tool for version control, helping developers keep track of changes in their code and collaborate effectively. One handy feature is the git commit --amend command, which allows you to modify your most recent commit. In this article, we will see how to Undo “git commit –amend".

Understanding the Problem

When you use git commit --amend, Git replaces your last commit with a new one. This means your previous commit is no longer part of your branch history. If you want to undo this amended commit, you must return the original one.

What Does git commit --amend Do?

When you run git commit --amend, Git takes your last commit and creates a new commit object that contains the changes of the previous commit along with any additional changes you have made in the working directory or staging area. The original commit is effectively replaced by this new commit, and it's hash (identifier) changes.

This means the history of your branch is rewritten, which can have implications if the commit has already been shared with others.

Steps to Undo "git commit --amend"

Step 1. Check Your Commit History

First, review your commit history to identify the commit before the one you amended. Open your terminal or command prompt and run:

git log

This command lists all commits, with the latest one at the top. Each commit has a unique hash identifier.

Step 2. Identify the Previous Commit

Find the hash of the commit that was just before the one you amended (git commit --amend). Let's say the hash of your amended commit is 12efc688d0d3a4b4a22a5d46c94f97982a8244ac, and the previous commit's hash is 9067b5b68a7f858fe20589988681cdde8cdf932d.

Step 3. Reset to the Previous Commit

Use the git reset command to revert to the state before the amend. Depending on whether you want to keep your changes in the working directory or not, choose one of the following options:

To discard all changes (including those in the working directory) and revert to the commit before git commit --amend, use:

git reset --hard 9067b5b68a7f858fe20589988681cdde8cdf932d

Warning: git reset --hard resets both your repository's commit history and working directory to the specified commit. Any changes made after this commit will be lost.

Step 4. To keep changes in your working directory while resetting the commit history, use:

 git reset --soft 9067b5b68a7f858fe20589988681cdde8cdf932d

This preserves changes in your working directory as unstaged changes.

Step 5. Verify Your Commit History

After resetting, verify your commit history again to ensure you've successfully reverted to the desired commit:

git log

You should now see the commit history has reverted to the commit identified by 9067b5b68a7f858fe20589988681cdde8cdf932d.

Example

1. Check Your History

First, let's see the commits in your repository. You can use:

git log
WhatsApp-Image-2024-06-06-at-102813-AM
git log

This command shows a list of commits, with the latest one at the top. Each commit has a unique identifier (hash).

2. Find the Previous Commit

You need to find the commit before the one you amended. Let's say your latest commit (the amended one) has the hash 12efc688d0d3a4b4a22a5d46c94f97982a8244ac and the one before that is 9067b5b68a7f858fe20589988681cdde8cdf932d.

3. Reset to the Previous Commit

Now, you can use the git reset command to undo the amend. To reset to the previous commit, run:

git reset --hard 9067b5b68a7f858fe20589988681cdde8cdf932d
Screenshot-from-2024-06-01-08-50-24
back to previous commit

This command will reset your branch to the commit 9067b5b68a7f858fe20589988681cdde8cdf932d, effectively undoing the amended commit.

WARNING: git reset --hard will also reset your working directory to the state of 9067b5b68a7f858fe20589988681cdde8cdf932d, meaning any changes you made after that commit will be lost. If you want to keep your working directory changes, use:

git reset --soft 9067b5b68a7f858fe20589988681cdde8cdf932d

This way, your changes will be kept, but the commit history will be reset to 9067b5b68a7f858fe20589988681cdde8cdf932d.

4. Check Your History Again

After resetting, it’s good practice to check your commit history again to ensure everything is as expected. Use:

git log
WhatsApp-Image-2024-06-06-at-102812-AM
git log

You should now see that the latest commit is 9067b5b68a7f858fe20589988681cdde8cdf932d.

Undoing Safely

1. Create a Backup Branch

Before you start undoing commits, it’s a good idea to create a backup branch. This way, you can always go back if something goes wrong. To create a backup branch, run:

git branch backup

Now, you can safely reset your main branch, knowing you have a backup.

2. Using Git Reflog

If you realize you made a mistake after resetting, you can use git reflog to recover. git reflog keeps track of all your actions in the repository. To see your reflog, run:

git reflog
WhatsApp-Image-2024-06-06-at-102813-AM(1)
git reflog

You’ll see a list of actions, each with a hash. Find the hash of the commit before your reset and use git reset to go back to that state.

Summary

Undoing a git commit --amend can seem tricky, but it's manageable with a few steps:

  1. Check your commit history with git log.
  2. Find the commit before your amended one.
  3. Reset to the previous commit using git reset --hard or git reset --soft.
  4. Create a backup branch to ensure safety.
  5. Use git reflog if you need to recover from mistakes.

Conclusion:

By following these steps, you can confidently undo an amended commit and keep your project history clean and accurate. Git’s flexibility and powerful features, like reflog, provide robust safety nets that allow you to correct mistakes and maintain control over your project’s history.

Whether you're a beginner or an experienced developer, understanding how to undo a git commit --amend ensures you can work more effectively and with greater confidence.


Next Article
How to Undo “git commit –amend"?

I

isandeep2183
Improve
Article Tags :
  • Web Technologies
  • Git

Similar Reads

    How to Undo a Commit in Git ?
    Git offers several ways to backtrack and correct mistakes, making it a powerful tool for developers. In this article, we will explore various methods to undo a commit in Git, ensuring that you can choose the best approach for your specific system.Below are the approaches to Undo a commit in Git:Tabl
    3 min read
    How to Undo 'git add' Before Commit?
    To undo a 'git add' before committing, you can use the 'git reset' command. This command unstages the changes that you previously added with 'git add', but it doesn't discard the changes from your working directory. This allows you to make further modifications to the files or decide later whether y
    3 min read
    How to Undo Last Commit in Git?
    Sometimes, you might need to undo the last commit, whether it's due to a mistake, an incorrect commit message, or the necessity to rework the changes. This article will guide you through different methods to uncommit the last commit in Git, ensuring you can manage your repository effectively. Need t
    3 min read
    How to Revert a Git Commit?
    In software development, mistakes happen, and there will be times when you need to undo changes in your Git repository. Reverting a Git commit is a common task that can be handled in several ways, depending on your needs and the state of your project. This article will guide you through the differen
    4 min read
    How To Amend Commit Message In Git?
    Sometimes, after making a commit in Git, you may realize that the commit message needs to be changed. Whether it's a typo, missing information, or a need for better clarity, Git provides a way to amend commit messages. This article will guide you through the process of amending commit messages in Gi
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences