Remove a Git Commit Which Has Not Been Pushed

Better Stack Team
Updated on August 12, 2024

If you want to remove a commit that has not been pushed to the remote repository, you have several options. The right approach depends on whether you want to discard the changes entirely or keep them but modify the commit history.

Option 1: Remove the Last Commit

If you want to completely remove the last commit, you can use the git reset command.

Hard Reset (completely remove changes)

This will remove the commit and discard the changes.

 
git reset --hard HEAD~1
  • -hard: Resets the working directory and the index to the state of the previous commit.
  • HEAD~1: Refers to the commit just before the current HEAD.

Soft Reset (keep changes in the working directory)

This will remove the commit but keep the changes in your working directory.

 
git reset --soft HEAD~1
  • -soft: Resets the index but leaves the working directory unchanged. Your changes will be staged and ready for a new commit.

Option 2: Remove an Older Commit

If you need to remove a specific commit that is not the latest, you can use an interactive rebase.

  1. Start an interactive rebase:

     
    git rebase -i HEAD~n
    

    Replace n with the number of commits you want to go back. For example, if the commit you want to remove is 3 commits back, use HEAD~3.

  2. In the editor that opens, find the commit you want to remove. Delete the entire line for that commit or change pick to drop in front of the commit hash.

    Example:

     
    pick a1b2c3d Commit message 1
    drop d4e5f6g Commit message 2
    pick h7i8j9k Commit message 3
    
  3. Save and close the editor to apply the rebase. Git will reapply the commits without the one you removed.

Option 3: Amend the Last Commit

If you simply want to modify the last commit (e.g., change its message or include additional changes), you can use git commit --amend.

To change the commit message:

 
git commit --amend

This will open your default text editor, allowing you to change the commit message.

To include additional changes:

  1. Stage the additional changes:

     
    git add <file>
    
  2. Amend the commit:

     
    git commit --amend
    

This will include the staged changes in the last commit.

Summary

  • Hard Reset: git reset --hard HEAD~1 - Completely remove the last commit and discard changes.
  • Soft Reset: git reset --soft HEAD~1 - Remove the last commit but keep changes in the working directory.
  • Interactive Rebase: git rebase -i HEAD~n - Remove a specific older commit.
  • Amend Commit: git commit --amend - Modify the last commit.

These commands will help you remove or alter commits that have not been pushed to the remote repository. Be cautious with these operations, as they modify the commit history.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github