How to Delete Commit in Git?
Last Updated :
20 May, 2024
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:
git reset --hard HEAD~1
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:
git rebase -i HEAD~3
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.
Similar Reads
How to Delete Last Commit in Git? Sometimes, you may want to delete your last commit because of mistakes, accidentally adding sensitive information, or just wanting to change your commit history. Here are the three ways to delete the last commit in Git-1. Delete the Last Commit and Keep the ChangesThis method removes the last commit
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