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 Force Commit in Git?
Next article icon

How to Delete Commit in Git?

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

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.

Deleting the Most Recent Commit

If the commit only exists in your local repository and you want to delete the most recent commit, use:

git reset --hard HEAD~1
  • `HEAD~1` refers to the commit immediately before the latest one.
  • `--hard` resets your working directory to match the commit, discarding any changes in your working directory and staging area.

Caution: This will delete any uncommitted changes. Ensure you commit or stash changes you want to keep before running this command.

If the commit has already been pushed to a remote repository, you’ll need to force push the changes:

git push origin HEAD --force

Warning: Force-pushing rewrites history on the remote branch, which can affect collaborators. Communicate with your team before doing this.

Deleting an Older Commit

If you need to delete an older commit, you can use an interactive rebase:

  • Start an interactive rebase:
   git rebase -i HEAD~N

Note: Replace `N` with the number of commits you want to go back.

  • In the interactive editor that opens, find the commit you want to delete and remove its line.
  • Save and close the editor. Git will rewrite the history without the deleted commit.
  • Force push the updated history to the remote repository:
   git push origin HEAD --force

Using `git revert` for a Safer Alternative

If the commit has already been pushed to a shared repository, a safer approach is to use `git revert`. This command creates a new commit that undoes the changes of a specified commit without rewriting history.

To revert a specific commit:

git revert <commit_hash>

Replace `<commit_hash>` with the hash of the commit you want to revert. This creates a new commit that reverses the changes, preserving the commit history.

Example Scenario

Let’s say you want to delete the most recent commit:

  • Run:
 git reset --hard HEAD~1

gith
git reset

  • If the commit was pushed to the remote repository, force push the changes:
   git push origin main --force

For deleting an older commit, assuming the last 3 commits:

  • Run:
   git rebase -i HEAD~3
gitr
git rebase


  • In the editor, delete the line corresponding to the commit you want to remove.
  • Save and close the editor.
  • Force push the changes:
   git push origin main --force

Summary

  • `git reset --hard HEAD~1`: Deletes the most recent commit locally.
  • `git rebase -i HEAD~N`: Deletes an older commit interactively.
  • Force pushing: Necessary when the commits have been pushed to a remote repository (`git push origin HEAD --force`).
  • `git revert <commit_hash>`: Safer alternative for shared repositories, preserving history by creating a new commit that undoes the specified commit.

Use these commands responsibly, especially when working with shared repositories, to avoid disrupting your team's workflow.


Next Article
How to Force Commit in Git?
author
htomarec8c
Improve
Article Tags :
  • Web Technologies
  • Git
  • Node-npm

Similar Reads

  • How to Delete Last Commit in Git?
    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, w
    4 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 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 Delete a Branch in Git?
    When working with Git, it’s common to create branches to isolate changes and work on new features or fixes. However, once a branch has served its purpose, it’s often necessary to delete it to keep your repository clean and organized. In this article, we’ll see the process of deleting a Git branch us
    3 min read
  • How to Back Commit in Git?
    In this article, we are covering how to undo commits in Git. Sometimes, you might make mistakes or realize you need to revert changes you made in a previous commit. Luckily, Git offers a few ways to undo commits. Table of Content Approach 1: Using 'git revert'Approach 2: Using 'git reset' Approach 1
    4 min read
  • How to Delete Branch in Github?
    In Git repositories, branches are important for organizing and managing code changes, but over time, unused or outdated branches can clutter the repository. Deleting these branches helps maintain a clean and manageable codebase. In this article, we'll explore the process of deleting branches in GitH
    2 min read
  • How to Delete Branch in Gitlab?
    When working with Git, it's common to create branches to isolate changes and work on new features or fixes. However, once a branch has served its purpose, it's often necessary to delete it to keep your repository clean and organized. In this article, we'll see the process of deleting a Git branch us
    2 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
  • 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 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: Ta
    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