Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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 Delete Commit in Git?
Next article icon

How to Delete Last Commit in Git?

Last Updated : 21 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Git, the popular version control system, provides rich tools for managing changes in your codebase. Sometimes, you may need to delete the last commit due to an error, unnecessary changes, or other reasons. In this article, we will walk you through the steps to safely delete the last commit in Git, whether the commit has been pushed to a remote repository or not.

Table of Content

  • Understanding Commit Deletion
  • 1. Deleting the Last Commit Locally
  • 2. Deleting the Last Commit Pushed to a Remote Repository
  • Best Practices and Considerations
  • Conclusion

Understanding Commit Deletion

Deleting the last commit in Git can mean different things depending on your specific needs:

  • Amending the Last Commit: Fixing or modifying the last commit without changing its overall identity.
  • Reverting the Last Commit: Creating a new commit that undoes the changes introduced by the previous commit.
  • Resetting to a Previous State: Completely removing the last commit from the history.

1. Deleting the Last Commit Locally

Using `git reset`

The `git reset` command is a powerful tool that allows you to move the HEAD pointer and optionally modify the index and working directory. To delete the last commit, you typically use the `--hard` or `--soft` options.

Example: Using `--hard` Reset

This option resets the HEAD to the previous commit and discards all changes in the working directory and index.

git reset --hard HEAD~1

This command moves the HEAD to the commit before the last one and removes all changes introduced by the last commit from the working directory and staging area.

Example: Using `--soft` Reset

This option resets the HEAD to the previous commit but keeps the changes in the staging area.

git reset --soft HEAD~1

This command moves the HEAD to the commit before the last one but retains the changes introduced by the last commit in the index (staging area), allowing you to make additional modifications before committing again.

Using `git revert`

If you want to keep a record of the changes but negate the effects of the last commit, you can use the `git revert` command. This creates a new commit that reverses the changes introduced by the last commit.

git revert HEAD

This command generates a new commit that undoes the changes of the last commit, preserving the history of changes.

2. Deleting the Last Commit Pushed to a Remote Repository

If the last commit has already been pushed to a remote repository, you need to be more cautious. Changing the commit history in a remote repository can affect other collaborators. Ensure that you communicate with your team before making such changes.

Using `git reset` and Force Push

You can use `git reset` to move the HEAD to the previous commit and then force push the changes to the remote repository.

Example: Force Pushing After `--hard` Reset

git reset --hard HEAD~1
git push origin main --force

This command sequence resets the HEAD locally and then force pushes the updated history to the remote repository, effectively deleting the last commit.

Using `git revert` and Push

To avoid rewriting history, use `git revert` followed by a regular push. This method is safer for shared repositories.

git revert HEAD
git push origin main

This command sequence creates a new commit that negates the last commit and then pushes the changes to the remote repository.

Best Practices and Considerations

Communicate with Your Team

When working in a team, always communicate with your colleagues before making changes to the commit history. This helps avoid confusion and conflicts.

Use Branches

Perform risky operations on a separate branch before applying them to the main branch. This allows you to verify the changes without affecting the primary workflow.

Backup Your Work

Before performing any destructive operations, ensure you have a backup of your work. You can create a temporary branch pointing to the current state:

git branch backup-branch

Understand the Impact

Force pushing can overwrite history and potentially disrupt the work of others. Use it with caution and understand its impact.

Conclusion

Deleting the last commit in Git is a common task that can be achieved through various methods depending on your needs. Whether you choose to reset, revert, or amend, understanding these techniques helps you maintain a clean and accurate commit history. Always exercise caution, especially when dealing with remote repositories, and communicate with your team to ensure a smooth workflow.


Next Article
How to Delete Commit in Git?

S

swati4934
Improve
Article Tags :
  • Web Technologies
  • Git

Similar Reads

  • How to Delete Commit in Git?
    Deleting a commit in Git can be done in several ways, depending on whether the commit is local or has already been pushed to a remote repository. Here’s an article on how to delete a commit in Git, covering both recent and older commits, as well as considerations for working with remote repositories
    3 min read
  • How to Delete Local Commit in Git?
    Deleting a local commit in Git can be done in a few ways, depending on whether the commit is the most recent one or if it is further back in the commit history. Here’s an article on how to delete a local commit in Git, tailored to whether you want to keep the changes made in the commit or discard th
    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 Get List of All Commits in Git?
    Git a powerful version control system, keeps track of all changes made to a project by recording each change in a commit. Sometimes, you need to see the complete list of commits to understand the project's history, track changes, or debug issues. Listing all commits in a Git repository can be useful
    3 min read
  • How to Squash Commits in Git?
    Maintaining a clean and organized Git history is very important for collaboration and project management. One way to streamline your commit history is by squashing commits, which combines multiple commits into a single, more coherent commit. In this article, we will see how to squash commits in Git.
    2 min read
  • How to Delete Local Branch in Git?
    Git is a widely used distributed version control and source code management system. It effectively tracks changes to source code, enabling effortless branching, merging, and versioning. What are Local Branches? In Git, branches are lightweight pointers to commits in the repository's history. Each br
    1 min read
  • How to Push an Empty Commit in Git?
    In Git, commits are snapshots of your repository at specific points in time, typically containing changes to the files. However, there are instances when you might need to create and push an empty commit, one that doesn’t introduce any changes to the codebase. This article explains why you might nee
    3 min read
  • How to Force Commit in Git?
    Git is a powerful version control system used by developers worldwide to manage and track changes in their codebases. However, there are times when you might need to force commit changes in Git, overriding previous commits or pushing changes to a remote repository with conflicts. This guide will exp
    3 min read
  • How to list All Files in a Commit in Git?
    Working with Git, it's often essential to inspect the contents of your commits. Whether you need to review changes, debug issues, or understand the history of your project, knowing how to list all files in a commit is a fundamental skill. In this article, we’ll walk you through the steps to list all
    3 min read
  • How do I Delete Unpushed Git Commits?
    Sometimes you might make mistakes in your Git commits and want to start fresh. Here's how to remove unpushed commits in Git, focusing on two clear methods. Table of Content Using Soft ResetUsing Hard ResetDiscard Unpushed Commits CompletelyPreserve Changes from Unpushed CommitsUsing Soft ResetUse th
    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