How to Remove a Commit in Git (Undo, Delete or Revert) with Examples

How to Remove a Commit in Git (Undo, Delete or Revert) with Examples

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

TaskCommandDescription
Undo last commit (keep changes staged)git reset --soft HEAD~1Moves HEAD back one commit but keeps all changes staged so they can be recommitted.
Undo last commit (keep changes unstaged)git reset --mixed HEAD~1Removes the last commit and moves the changes back to the working directory.
Delete last commit permanentlygit reset --hard HEAD~1Deletes the last commit and discards all associated changes permanently.
Undo commit before pushgit reset HEAD~1Removes the most recent commit locally before pushing it to a remote repository.
Fix last commit messagegit commit --amendEdits the message of the most recent commit without creating a new commit.
Remove specific commit from historygit rebase -i HEAD~NOpens interactive rebase allowing you to drop or modify specific commits from the last N commits.
Remove multiple commitsgit reset --hard HEAD~NRemoves the last N commits from the current branch history.
Remove commit from branchgit rebase -iAllows rewriting branch history to remove or modify commits interactively.
Undo pushed commit safelygit 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 --forceResets the branch to a previous commit and force pushes the rewritten history to the remote repository.
Remove merge commitgit revert -m 1 <commit>Reverts a merge commit by specifying which parent branch should remain.
Remove first commit in repositorygit update-ref -d HEADDeletes the HEAD reference, effectively removing the initial commit.
Remove all commits and historyrm -rf .gitDeletes the entire Git repository history by removing the .git directory.
Recover deleted commitgit reflogDisplays 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:

bash
git log

or a concise format:

bash
git log --oneline

Understanding 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:

text
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:

text
A → B → C → D

If commit D is removed, the history becomes:

text
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 TypeDescriptionRecommended Action
Local commitExists only in your local repositorySafe to remove using git reset
Pushed commitAlready shared with remote repositoryPrefer 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:

ReferenceMeaning
HEADCurrent commit
HEAD~1One commit before HEAD
HEAD~2Two commits before HEAD
<commit-hash>A specific commit identifier

For example:

bash
git reset --soft HEAD~1

This 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:

bash
git reset --soft HEAD~1

This 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:

bash
git reset --mixed HEAD~1

This 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:

bash
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:

bash
git revert HEAD

This 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:

bash
git reset HEAD~1

This 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:

bash
git reset --soft HEAD~1

You can then adjust the changes and commit again.

Fix commit message before push

If the commit message contains mistakes, you can modify it using:

bash
git commit --amend

This opens the editor where you can change the commit message.

Remove staged commit before push

If files were staged incorrectly before committing:

bash
git reset

This 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.

bash
git rebase -i HEAD~5

Git will open your default text editor showing something like this:

text
pick a1b2c3 First commit
pick d4e5f6 Second commit
pick g7h8i9 Third commit
pick j1k2l3 Fourth commit
pick m4n5o6 Fifth commit

Each line represents a commit.

To remove a commit, replace pick with drop for the commit you want to delete.

text
pick a1b2c3 First commit
drop d4e5f6 Second commit
pick g7h8i9 Third commit
pick j1k2l3 Fourth commit
pick m4n5o6 Fifth commit

Save 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:

text
pick a1b2c3 Initial commit
pick d4e5f6 Add configuration file
pick g7h8i9 Fix bug
pick j1k2l3 Update documentation

If the bug fix commit should be removed, modify the list like this:

text
pick a1b2c3 Initial commit
pick d4e5f6 Add configuration file
drop g7h8i9 Fix bug
pick j1k2l3 Update documentation

After 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:

bash
git rebase -i HEAD~N

Replace N with the number of commits you want to inspect.

Example:

text
pick a1b2c3 Commit message 1
pick d4e5f6 Commit message 2
pick g7h8i9 Commit message 3

To remove a commit:

text
pick a1b2c3 Commit message 1
drop d4e5f6 Commit message 2
pick g7h8i9 Commit message 3

After saving the file, Git rewrites the history without the removed commit.

You can confirm the updated commit history using:

bash
git log --oneline

Rewrite 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.

bash
git push --force

A safer alternative is:

bash
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.

bash
git reset --hard HEAD~3

Explanation:

  • HEAD~3 moves the pointer three commits backward
  • --hard removes the commits and discards all associated changes

If you want to keep the changes but remove the commits, use:

bash
git reset --soft HEAD~3

This 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.

bash
git rebase -i HEAD~5

This opens an editor listing the last five commits:

text
pick 5a8e1c3 commit message 1
pick 7d2f4a9 commit message 2
pick 9e3c8f1 commit message 3

You can replace pick with:

text
drop

for 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:

bash
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:

text
A → B → C → D → E (HEAD)

After resetting to commit C:

text
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:

bash
git rebase -i HEAD~5

Available actions include:

ActionPurpose
pickkeep the commit
dropremove the commit
squashcombine commits
editmodify 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.

The safest way to undo a pushed commit is by creating a new commit that reverses the changes.

bash
git revert HEAD

This does not modify history. Instead, it adds a new commit that cancels the previous one.

Example history:

text
A → B → C (bad commit)

After revert:

text
A → B → C → D

Where 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.

bash
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:

bash
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:

bash
git reset --hard HEAD~1

This 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:

bash
git checkout branch-name

Then remove the commit:

bash
git reset --hard HEAD~1

Remove commit after merging a branch

If a commit was introduced through a merge and you want to undo it, use:

bash
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:

bash
git rebase -i main

This 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:

bash
git reset --hard <commit-hash>

Then force push the updated history:

bash
git push origin branch-name --force

Remove commit from remote branch

To remove a commit from a specific remote branch:

bash
git push origin branch-name --force

This 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:

bash
git reset --hard HEAD~2
git push --force

This 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:

bash
git update-ref -d HEAD

This 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.

bash
rm -rf .git

This removes the entire commit history and Git configuration.

Reinitialize repository without commit history

After deleting .git, you can reinitialize the repository:

bash
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:

bash
git reset --soft HEAD~1

This removes the last commit but keeps all changes staged.

Another example:

bash
git reset --hard HEAD~1

This 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:

bash
git revert HEAD

This creates a new commit that reverses the previous commit instead of deleting it.

Example history:

text
A → B → C

After reverting commit C:

text
A → B → C → D

Commit 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:

bash
git rebase -i HEAD~5

This 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

SituationRecommended Command
Undo last commit locallygit reset --soft HEAD~1
Remove last commit permanentlygit reset --hard HEAD~1
Undo pushed commit safelygit revert <commit>
Remove specific commit from historygit rebase -i
Clean up multiple commitsgit 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:

bash
git reset --hard

can permanently delete commits and associated changes.

Always verify the commit history first:

bash
git log

or create a backup branch before modifying history:

bash
git branch backup-branch

Losing 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:

bash
git reset --soft

or

bash
git reset --mixed

Force pushing shared branches

Using:

bash
git push --force

can 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:

bash
git revert

instead 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:

bash
git branch

or

bash
git status

How 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:

bash
git reflog

This shows a list of previous commit references.

Once you find the commit hash, you can restore it:

bash
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:

bash
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:

bash
git reflog

Then reset the branch back:

bash
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:

bash
git reflog

If 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 using git 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 use git 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. Run git 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 using git 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 using git 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:

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.