How Do I Delete Unpushed Git Commits?

Better Stack Team
Updated on July 25, 2024

To delete unpushed Git commits, you have a few options depending on your specific situation and how you want to handle the commits. Here are some common scenarios and methods to delete unpushed commits:

Scenario 1: Delete Local Commits Completely

If you have made commits locally that you no longer want to keep, and they haven't been pushed to any remote repository:

  1. Reset the Branch: Use git reset to move the branch pointer to a previous commit, effectively removing the commits from your current branch history. Be cautious with this command as it will remove the commits entirely from your local repository history.

     
    git reset --hard HEAD~N
    

    Replace N with the number of commits you want to remove. This command resets your current branch to N commits before the current HEAD, discarding all changes and commits made after that point.

  2. Force Push (Optional, if necessary): If you've previously pushed these commits to a remote repository and need to remove them there as well (this can cause issues for collaborators if they have based work on these commits):

     
    git push origin <branch-name> --force
    

    Note: Force pushing can overwrite changes on the remote repository. Only use this command if you are certain of the consequences.

Scenario 2: Delete Specific Unpushed Commits

If you want to remove specific unpushed commits from your current branch history:

  1. Use git rebase -i: Interactive rebase allows you to interactively choose which commits to keep, edit, or delete. Start an interactive rebase session:

     
    git rebase -i HEAD~N
    

    Replace N with the number of commits you want to review and potentially delete.

  2. Follow the Interactive Rebase Instructions: In the interactive rebase editor that opens, change the command from pick to drop or delete the lines for the commits you want to remove. Save and exit the editor.

  3. Finish the Rebase: Git will proceed with the rebase, removing the specified commits from your branch history.

  4. Force Push (if necessary): If you've already pushed these commits and need to update the remote repository with the new history:

     
    git push origin <branch-name> --force
    

Notes and Considerations

  • Collaboration: If you're working in a team, communicate with your team members before force pushing to avoid disrupting their work.
  • Backup: Before performing actions that rewrite history (reset --hard or rebase -i followed by force push), ensure you have backups or copies of any important changes.
  • Be Cautious: Rewriting history can cause problems if others have based their work on the commits you intend to delete. It's a good practice to avoid rewriting history on branches that are shared and actively worked on by others.

By carefully following these steps, you can delete unpushed Git commits based on your specific needs and circumstances. Always consider the impact on collaborators and follow best practices to avoid unintended consequences.

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