How to cancel a local git commit?

40    Asked by RuthLambert in Devops , Asked on Jun 2, 2025

How do you cancel or undo a local Git commit that hasn't been pushed yet?

What are the different ways to modify or remove the last commit safely in Git?

Answered by mariakenneth

Canceling a local Git commit is a common task, especially when you've made a mistake or need to adjust something before pushing your changes. The good news is that Git provides flexible ways to undo commits—whether you want to keep your changes or discard them completely.

1. To undo the last commit but keep the changes:

Use this if you just want to "uncommit" but retain the code in your working directory.

  git reset --soft HEAD~1

  • Moves the commit back, but keeps staged changes.
  • Great for editing the commit or making additional changes before recommitting.

2. To undo the last commit and unstage the changes:

Use this when you want to keep the code but not have it staged.

  git reset --mixed HEAD~1

  • Keeps the files, but unstages them.
  • Useful if you want to reselect what to commit.

3. To undo the last commit and delete the changes:

Use with caution—this removes everything related to the commit.

  git reset --hard HEAD~1

  • Deletes the commit and your changes completely.
  • Only use this if you're 100% sure you don’t need the changes.

Final Tip:

Always double-check whether the commit has been pushed to a remote. These commands are safe for local-only commits. If you’ve already pushed, you’ll need to be more careful, or use git revert instead to maintain a clean history.



Your Answer

Interviews

Parent Categories