LogIn
I don't have account.

How can I undo the latest local commit on Git ?

I made some incorrect file commits to Git, but I haven't pushed them to the server yet.
How can I undo those commits from the local repository?

Please pay attention to update your answer.

Update Your Answer care fully. After saving you can not recover your old answer

Carefully Update Your Answer!!

Undo a Commit


1. git reset --soft HEAD~1

This is used in Git to reset the current branch's HEAD pointer to the previous commit and move all changes from the latest commit in the working directory and index (staging area). 

2. git reset --soft HEAD~2 

It's similar to the previous one (git reset --soft HEAD~1), but it goes back two commits instead of just one. similarly you can undo as many commit as you want.

3. git reset --hard HEAD~1 

This is aslo used in Git to reset the current branch's HEAD pointer to the previous commit but in place of moving latest commits in staging area it will removes all changes made in the last commit and resets the working directory and staging area to the state of the previous commit. 

4. git reset --hard HEAD~2 

It's similar to the previous one (git reset --hard HEAD~1), but it goes back two commits instead of just one. similarly you can undo as many commit as you want.
 

Please pay attention to update your answer.

Update Your Answer care fully. After saving you can not recover your old answer

Carefully Update Your Answer!!

Undo last commit but keep changes (staged)

git reset --soft HEAD~1

Undo last commit but keep changes (unstaged)

git reset --mixed HEAD~1

Undo last commit and discard changes

git reset --hard HEAD~1

Use --soft or --mixed if you haven’t pushed yet and want to fix files before recommitting.

Please pay attention to update your answer.

Update Your Answer care fully. After saving you can not recover your old answer

Carefully Update Your Answer!!

If you haven’t pushed your commit yet, you can undo it safely using:

  • Keep changes:

    git reset --soft HEAD~1
    
  • Keep changes unstaged:

    git reset HEAD~1
    
  • Remove commit and changes completely:

    git reset --hard HEAD~1
  1. Use --soft to fix and recommit
  2. Use --hard only if you want to discard everything.

Please pay attention to update your answer.

Update Your Answer care fully. After saving you can not recover your old answer

Carefully Update Your Answer!!

Undo the last commit safely (without reset)

git revert HEAD

This creates a new commit that undoes the changes made in the last commit.
It’s safer because it doesn’t modify history, useful even if you had pushed (though you haven’t yet).

If you just want to remove the last commit (no new commit)

git reset --soft HEAD~1

Summary:

  • Use git revert HEAD :- to undo safely (creates a new commit).
  • Use git reset :- to remove the commit entirely (changes history)