How do I undo the most recent local commits in Git?

35    Asked by adelin_7962 in Devops , Asked on Mar 18, 2025

You can undo the last commit in Git using git reset. Use --soft to keep changes staged, --mixed to unstage them, or --hard to discard them completely.

Answered by Diya tomar

To undo the most recent local commit in Git, you have a few options depending on what you want to do with the changes.

1. Keep the changes staged (Undo commit but keep files staged)

Use:

  git reset --soft HEAD~1

This removes the commit but keeps your changes staged, so you can modify and commit them again.

2. Keep the changes but unstage them (Undo commit and unstage files)

Use:

  git reset --mixed HEAD~1

This removes the commit and unstages your changes, so they remain in your working directory. You’ll need to add them again before committing.

3. Completely remove the commit and changes (Undo commit and delete changes)

Use:

  git reset --hard HEAD~1

Warning: This will permanently delete your changes from the last commit. Use this only if you’re sure you don’t need them.

4. Undo multiple commits

To undo multiple commits, replace HEAD~1 with the number of commits you want to remove:

  git reset --soft HEAD~3  # Undo last 3 commits

5. Undo a pushed commit

If you've already pushed the commit, you'll need to force push:

  git push origin main --force

  • Be careful when using --force, as it rewrites history and can affect team members' work.
  • Choose the right method based on whether you want to keep, unstage, or discard your changes.



Your Answer

Interviews

Parent Categories