Git allows you to rewrite history and remove commits when something goes wrong.
Depending on the situation, you may want to undo the last commit, remove a specific commit from history, or revert a pushed commit safely without breaking the repository.
This guide explains the different ways to remove commits in Git using commands like git reset, git revert, and git rebase, along with practical scenarios and examples.
Git Remove Commit – Quick Command Cheat Sheet
| Task | Command | Description |
|---|---|---|
| Undo last commit (keep changes staged) | git reset --soft HEAD~1 | Moves HEAD back one commit but keeps all changes staged so they can be recommitted. |
| Undo last commit (keep changes unstaged) | git reset --mixed HEAD~1 | Removes the last commit and moves the changes back to the working directory. |
| Delete last commit permanently | git reset --hard HEAD~1 | Deletes the last commit and discards all associated changes permanently. |
| Undo commit before push | git reset HEAD~1 | Removes the most recent commit locally before pushing it to a remote repository. |
| Fix last commit message | git commit --amend | Edits the message of the most recent commit without creating a new commit. |
| Remove specific commit from history | git rebase -i HEAD~N | Opens interactive rebase allowing you to drop or modify specific commits from the last N commits. |
| Remove multiple commits | git reset --hard HEAD~N | Removes the last N commits from the current branch history. |
| Remove commit from branch | git rebase -i | Allows rewriting branch history to remove or modify commits interactively. |
| Undo pushed commit safely | git revert <commit> | Creates a new commit that reverses the changes from the specified commit without rewriting history. |
| Remove pushed commit (rewrite history) | git reset --hard <commit> + git push --force | Resets the branch to a previous commit and force pushes the rewritten history to the remote repository. |
| Remove merge commit | git revert -m 1 <commit> | Reverts a merge commit by specifying which parent branch should remain. |
| Remove first commit in repository | git update-ref -d HEAD | Deletes the HEAD reference, effectively removing the initial commit. |
| Remove all commits and history | rm -rf .git | Deletes the entire Git repository history by removing the .git directory. |
| Recover deleted commit | git reflog | Displays reference history allowing recovery of commits that were removed or reset. |
The sections below in the article explain each scenario from the above table in detail with practical Git examples.
Understanding Git Commits Before Removing Them
Before removing a commit, it is important to understand how Git stores changes and how commit history works. Git keeps a record of every commit along with a unique identifier (commit hash), which allows you to modify or undo changes safely when necessary.
What a Git commit contains
A Git commit represents a snapshot of your project at a specific point in time. Each commit includes:
- A unique commit hash (SHA) used to identify the commit
- The author name and email
- The timestamp when the commit was created
- The commit message describing the changes
- The changes to files compared with the previous commit
You can view commit details using:
git logor a concise format:
git log --onelineUnderstanding the commit structure helps determine which commit should be removed or modified.
How Git commit history works
Git maintains a linear history of commits, where each commit points to its parent commit. This structure allows Git to track the evolution of the project.
A simplified commit history may look like:
A → B → C → D (HEAD)Here:
- A is the initial commit
- B, C, and D are subsequent commits
- HEAD points to the most recent commit
When removing commits, Git essentially moves the HEAD pointer or rewrites part of the commit history.
For example:
A → B → C → DIf commit D is removed, the history becomes:
A → B → C (HEAD)Difference between local commits and pushed commits
Understanding whether a commit has been pushed to a remote repository is critical before removing it.
| Commit Type | Description | Recommended Action |
|---|---|---|
| Local commit | Exists only in your local repository | Safe to remove using git reset |
| Pushed commit | Already shared with remote repository | Prefer git revert to avoid rewriting history |
If you modify history after pushing commits, you may disrupt other developers' work unless you force push changes.
Understanding HEAD and commit references
In Git, HEAD is a pointer to the currently checked-out commit.
Common references used when modifying commits include:
| Reference | Meaning |
|---|---|
HEAD | Current commit |
HEAD~1 | One commit before HEAD |
HEAD~2 | Two commits before HEAD |
<commit-hash> | A specific commit identifier |
For example:
git reset --soft HEAD~1This command moves HEAD back one commit while preserving the changes.
Understanding these references helps you safely remove, undo, or rewrite commits.
Remove the Last Commit in Git
Removing the last commit is one of the most common tasks when working with Git. This situation typically occurs when you accidentally commit incorrect changes or forget to include certain files.
Remove last commit but keep changes staged
If you want to undo the last commit but keep all changes staged, use:
git reset --soft HEAD~1This command removes the commit but keeps the files in the staging area, allowing you to recommit them.
Remove last commit but keep changes in working directory
If you want to undo the commit but move the changes back to the working directory:
git reset --mixed HEAD~1This removes the commit and unstages the files, but your changes remain intact.
Remove last commit and discard changes permanently
If you want to delete the commit and all associated changes:
git reset --hard HEAD~1⚠️ Use this carefully because it permanently deletes the commit and file changes.
Undo last commit safely in collaborative repositories
If the commit has already been pushed to a shared repository, rewriting history may cause conflicts for other developers.
In such cases, use:
git revert HEADThis creates a new commit that reverses the previous changes instead of modifying history.
Undo a Commit Before Push
Sometimes you commit changes but realize the commit should not be pushed yet. In this case, it is safe to undo the commit locally.
Cancel a commit before pushing to remote
To cancel the most recent commit before pushing:
git reset HEAD~1This moves the changes back to the working directory.
Undo a commit but keep code changes
If you want to keep the changes staged for editing or modification:
git reset --soft HEAD~1You can then adjust the changes and commit again.
Fix commit message before push
If the commit message contains mistakes, you can modify it using:
git commit --amendThis opens the editor where you can change the commit message.
Remove staged commit before push
If files were staged incorrectly before committing:
git resetThis removes files from the staging area without deleting them.
Remove a Specific Commit From History
Sometimes you may need to remove a commit that is not the most recent commit in your Git history.
This commonly happens when a commit introduces an error, contains sensitive data, or should not be included in the final project history.
Git provides a powerful feature called interactive rebase, which allows you to modify, reorder, edit, or remove commits from the commit history.
Remove commit using interactive rebase
To remove a commit from the last few commits, use interactive rebase.
Example: open the last 5 commits in the editor.
git rebase -i HEAD~5Git will open your default text editor showing something like this:
pick a1b2c3 First commit
pick d4e5f6 Second commit
pick g7h8i9 Third commit
pick j1k2l3 Fourth commit
pick m4n5o6 Fifth commitEach line represents a commit.
To remove a commit, replace pick with drop for the commit you want to delete.
pick a1b2c3 First commit
drop d4e5f6 Second commit
pick g7h8i9 Third commit
pick j1k2l3 Fourth commit
pick m4n5o6 Fifth commitSave and close the editor.
Git will rewrite the commit history without the dropped commit.
Drop a commit from the middle of history
Interactive rebase also allows you to remove commits located in the middle of the commit history.
Example commit list:
pick a1b2c3 Initial commit
pick d4e5f6 Add configuration file
pick g7h8i9 Fix bug
pick j1k2l3 Update documentationIf the bug fix commit should be removed, modify the list like this:
pick a1b2c3 Initial commit
pick d4e5f6 Add configuration file
drop g7h8i9 Fix bug
pick j1k2l3 Update documentationAfter saving, Git removes that commit while preserving the rest of the commit history.
Remove commit while keeping other commits intact
Interactive rebase ensures that only the selected commit is removed, while the remaining commits remain unchanged.
Run:
git rebase -i HEAD~NReplace N with the number of commits you want to inspect.
Example:
pick a1b2c3 Commit message 1
pick d4e5f6 Commit message 2
pick g7h8i9 Commit message 3To remove a commit:
pick a1b2c3 Commit message 1
drop d4e5f6 Commit message 2
pick g7h8i9 Commit message 3After saving the file, Git rewrites the history without the removed commit.
You can confirm the updated commit history using:
git log --onelineRewrite Git history safely
Interactive rebase rewrites commit history. This is generally safe only if the commits have not yet been pushed to a remote repository.
If the commits are already pushed, updating the remote history requires a force push.
git push --forceA safer alternative is:
git push --force-with-lease⚠️ Rewriting public history can affect other developers working on the same branch.
Always coordinate with your team before using force push on shared repositories.
Remove Multiple Commits
Sometimes you may need to remove more than one commit from the repository history. This can happen when multiple incorrect commits were made, or when you want to clean up commit history before merging or pushing changes.
Remove last N commits
If you want to remove several recent commits, you can reset the branch to an earlier point.
Example: remove the last 3 commits.
git reset --hard HEAD~3Explanation:
HEAD~3moves the pointer three commits backward--hardremoves the commits and discards all associated changes
If you want to keep the changes but remove the commits, use:
git reset --soft HEAD~3This keeps all modifications staged so you can recommit them properly.
Remove commits from branch history
If you want to remove commits that are not necessarily the latest ones, interactive rebase is a better option.
git rebase -i HEAD~5This opens an editor listing the last five commits:
pick 5a8e1c3 commit message 1
pick 7d2f4a9 commit message 2
pick 9e3c8f1 commit message 3You can replace pick with:
dropfor the commit you want to remove.
After saving the file, Git rewrites the commit history without those commits.
Rewrite commit history using reset
Another approach is resetting the branch to a previous commit.
Example:
git reset --hard <commit-hash>This moves the current branch pointer to the specified commit and removes all commits after it.
Example history before reset:
A → B → C → D → E (HEAD)After resetting to commit C:
A → B → C (HEAD)Commits D and E are removed.
Remove commits using interactive rebase
Interactive rebase is useful when you want full control over commit history.
Start rebase:
git rebase -i HEAD~5Available actions include:
| Action | Purpose |
|---|---|
| pick | keep the commit |
| drop | remove the commit |
| squash | combine commits |
| edit | modify the commit |
This method is ideal for cleaning up commit history before merging branches.
Remove Commit After Push
If a commit has already been pushed to a remote repository, removing it requires extra caution because other developers may already have the commit.
Undo pushed commit using git revert (recommended)
The safest way to undo a pushed commit is by creating a new commit that reverses the changes.
git revert HEADThis does not modify history. Instead, it adds a new commit that cancels the previous one.
Example history:
A → B → C (bad commit)After revert:
A → B → C → DWhere commit D reverses the changes introduced in commit C.
Remove pushed commit using force push
If you want to completely remove the commit from history, you must reset the branch and force push.
git reset --hard HEAD~1
git push --force⚠️ Warning: this rewrites remote history and may disrupt collaborators.
Undo pushed commit without rewriting history
Using git revert is preferred when working with shared repositories.
Example:
git revert <commit-hash>This preserves history and prevents conflicts for other developers.
When not to rewrite public history
Avoid using git reset and git push --force when:
- the branch is shared with other developers
- the commit has already been merged
- the repository is public
In these cases, use git revert instead.
Remove Commit From a Branch
Sometimes you want to remove commits specifically from a branch while keeping the rest of the repository intact.
Remove commit from current branch
To remove the latest commit from the current branch:
git reset --hard HEAD~1This deletes the most recent commit from the branch.
Remove commit from another branch
If you want to remove a commit from another branch, first switch to that branch:
git checkout branch-nameThen remove the commit:
git reset --hard HEAD~1Remove commit after merging a branch
If a commit was introduced through a merge and you want to undo it, use:
git revert -m 1 <merge-commit-hash>The -m 1 option tells Git which parent branch should remain.
Clean up commit history before merging
Before merging a feature branch, developers often clean up commits using interactive rebase:
git rebase -i mainThis allows you to:
- remove unnecessary commits
- squash multiple commits into one
- improve commit messages
Remove Commit From Remote Repository
Sometimes you need to remove commits that exist on the remote repository (for example on GitHub or GitLab).
Remove commit from GitHub repository
First reset the local branch to the correct commit:
git reset --hard <commit-hash>Then force push the updated history:
git push origin branch-name --forceRemove commit from remote branch
To remove a commit from a specific remote branch:
git push origin branch-name --forceThis updates the remote branch to match the local branch.
Force push updated commit history
Force push replaces the remote history with the local one.
Example workflow:
git reset --hard HEAD~2
git push --forceThis removes the last two commits from the remote repository.
Risks of rewriting remote history
Rewriting remote history can cause problems such as:
- breaking other developers' repositories
- losing commits permanently
- creating merge conflicts
Because of these risks, it is often safer to use git revert
Remove the First Commit in a Repository
Removing the first commit requires special commands because it is the root of the commit history.
Remove initial commit using update-ref
You can delete the first commit reference using:
git update-ref -d HEADThis removes the commit pointer and returns the repository to an uncommitted state.
Delete repository history by removing .git
Another method is deleting the .git directory.
rm -rf .gitThis removes the entire commit history and Git configuration.
Reinitialize repository without commit history
After deleting .git, you can reinitialize the repository:
git init
git add .
git commit -m "Initial commit"This creates a new repository with fresh history.
Git Reset vs Git Revert vs Git Rebase
When removing commits in Git, three commands are commonly used: git reset, git revert, and git rebase. Each command modifies commit history in a different way, so choosing the correct one depends on whether the commit has been pushed and whether you want to rewrite history.
When to use git reset
Use git reset when you want to remove commits locally and adjust the commit history. This command moves the branch pointer to an earlier commit.
Example:
git reset --soft HEAD~1This removes the last commit but keeps all changes staged.
Another example:
git reset --hard HEAD~1This completely removes the last commit and discards the changes.
Use git reset when:
- commits have not been pushed yet
- you want to rewrite local commit history
- you want to remove recent commits quickly
However, avoid using git reset --hard if you need the changes later because it permanently deletes them.
When to use git revert
Use git revert when the commit has already been pushed to a shared repository and you want to undo its changes safely.
Example:
git revert HEADThis creates a new commit that reverses the previous commit instead of deleting it.
Example history:
A → B → CAfter reverting commit C:
A → B → C → DCommit D cancels the changes introduced by C.
Use git revert when:
- the commit has already been pushed to remote
- the repository is shared with other developers
- you want to preserve commit history
When to use git rebase
Use git rebase when you want to rewrite commit history or remove specific commits while keeping the rest of the history clean.
Example:
git rebase -i HEAD~5This opens an interactive editor where you can:
- drop commits
- squash commits
- edit commit messages
- reorder commits
Rebase is often used to clean up commit history before merging a branch.
Choosing the safest method to remove commits
| Situation | Recommended Command |
|---|---|
| Undo last commit locally | git reset --soft HEAD~1 |
| Remove last commit permanently | git reset --hard HEAD~1 |
| Undo pushed commit safely | git revert <commit> |
| Remove specific commit from history | git rebase -i |
| Clean up multiple commits | git rebase -i |
As a rule of thumb:
- Use reset for local commits
- Use revert for pushed commits
- Use rebase for cleaning or editing commit history
Common Mistakes When Removing Git Commits
Removing commits incorrectly can cause loss of work or conflicts with other developers. Below are common mistakes and how to avoid them.
Accidentally deleting commits
Using commands like:
git reset --hardcan permanently delete commits and associated changes.
Always verify the commit history first:
git logor create a backup branch before modifying history:
git branch backup-branchLosing changes after git reset --hard
The --hard option resets:
- commit history
- staging area
- working directory
If changes are not committed elsewhere, they may be lost permanently.
To avoid losing changes, use:
git reset --softor
git reset --mixedForce pushing shared branches
Using:
git push --forcecan overwrite commit history in remote repositories.
This may cause other developers to lose their work or encounter merge conflicts.
If commits were already pushed, prefer using:
git revertinstead of force pushing.
Removing commits from wrong branch
Sometimes developers accidentally remove commits from the wrong branch.
Always confirm the current branch before running destructive commands:
git branchor
git statusHow to Recover Deleted Git Commits
Even if a commit is removed, Git often keeps references that allow recovery.
Recover removed commit using git reflog
git reflog records updates to the HEAD pointer and can help restore lost commits.
Example:
git reflogThis shows a list of previous commit references.
Once you find the commit hash, you can restore it:
git checkout <commit-hash>Restore deleted commit using git checkout
If you know the commit hash, you can retrieve the commit by creating a new branch:
git checkout -b recovered-branch <commit-hash>This restores the commit history starting from that commit.
Recover branch after reset
If a branch was reset accidentally, use reflog to locate the previous commit:
git reflogThen reset the branch back:
git reset --hard <commit-hash>Restore lost commits after force push
If commits were lost due to force pushing, you may still recover them locally using:
git reflogIf the commits exist in another developer's repository, they can also be restored by fetching their branch.
Frequently Asked Questions
1. How do I remove the last commit in Git?
You can remove the last commit usinggit reset --soft HEAD~1 to keep changes staged, git reset --mixed HEAD~1 to keep changes in the working directory, or git reset --hard HEAD~1 to permanently delete the commit and its changes.2. How do I undo a commit that has already been pushed?
If the commit has already been pushed to a remote repository, the safest approach is to usegit revert <commit-hash>. This creates a new commit that reverses the changes without rewriting history.3. How do I remove a specific commit from Git history?
You can remove a specific commit using interactive rebase. Rungit rebase -i HEAD~N, locate the commit you want to remove, and change pick to drop. After saving, Git rewrites the commit history without that commit.4. Can I delete multiple commits at once in Git?
Yes. You can remove multiple commits usinggit reset --hard HEAD~N to delete the last N commits or use git rebase -i HEAD~N to selectively remove or edit commits.5. How can I recover a deleted Git commit?
If you accidentally remove a commit, you can recover it usinggit reflog. Find the commit hash in the reflog history and restore it using git checkout <commit-hash> or git reset --hard <commit-hash>.Summary
In this guide, we explored how to remove, undo, or delete commits in Git using different commands and workflows. Removing commits is a common task when correcting mistakes, cleaning up commit history, or undoing changes that should not be part of the repository.
The tutorial covered multiple scenarios including removing the last commit, undoing a commit before push, deleting specific commits from history, removing multiple commits, undoing pushed commits safely using git revert, and rewriting history using git reset or interactive git rebase.
We also discussed common mistakes developers make when removing commits, how to avoid rewriting public history incorrectly, and how to recover deleted commits using git reflog if something goes wrong.
Understanding these workflows ensures that Git commit history can be modified safely without losing important work or breaking collaboration across teams and shared repositories.
Official Documentation
You can learn more about removing commits and managing Git history from the official Git documentation:



![Git Error: Cannot Delete Branch Checked Out or Used by Worktree [SOLVED]](/cannot-delete-branch-checked-out-at/git-cannot-delete-branch_hu_3f86bd25a59d627d.webp)






