How to Throw Away Local Commits in Git

Better Stack Team
Updated on June 24, 2024

To discard or throw away local commits in Git, you have a few options depending on your specific scenario:

Option 1: Discard Uncommitted Changes

If you haven't yet committed your changes and want to discard them completely:

 
git reset --hard HEAD

This command will reset your working directory and staging area to match the HEAD commit, effectively discarding all uncommitted changes.

Option 2: Reset to a Specific Commit

If you've already committed your changes and want to discard all commits made after a certain commit:

 
git reset --hard <commit>

Replace <commit> with the commit hash or reference to which you want to reset. This will reset your branch pointer to the specified commit and discard all commits made after it.

Option 3: Create a New Branch without the Commits

If you want to keep your changes but don't want them in your current branch's history, you can create a new branch from a commit prior to the unwanted commits:

 
git checkout -b new-branch <commit>

Replace new-branch with the name of the new branch and <commit> with the commit hash or reference you want to base the new branch on. This will create a new branch without the unwanted commits.

Option 4: Use the Reflog

If you've already made commits and want to "throw away" them by reverting your branch to a previous state:

 
git reflog

Find the entry corresponding to the state you want to revert to and note its commit hash. Then, reset your branch to that commit:

 
git reset --hard <commit>

Replace <commit> with the commit hash you noted from the reflog. This will move your branch pointer back to the specified commit, effectively "throwing away" the commits made after it.

Note:

  • Be cautious when using these commands as they permanently discard commits and changes.
  • Make sure to back up any important changes before executing these commands, especially if you're unsure about the consequences.
  • If the commits you want to discard have already been pushed to a remote repository, you may need to use git push --force to update the remote branch with your changes.
Got an article suggestion? Let us know
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