Git undo Previous commit


There are different approaches to undo local commits in Git:

Using Git reset:

This command moves the current branch pointer to a specified commit, and optionally resets the staging area and working directory to match

.To undo the most recent commit and leave the changes staged:

git reset --soft HEAD~1

To undo the most recent commit and unstage the changes:

git reset HEAD~1

To undo the most recent commit and discard the changes:

git reset --hard HEAD~1

Git revert: To Undo the most recent commit

This command creates a new commit that undoes the changes introduced by a previous commit.

To undo the most recent commit:

git revert HEAD  
//This will open the default Git editor to create a commit message. To skip the editor, use the --no-edit option.

Git checkout

This command checks out a specific commit or branch, and updates the files in the working directory accordingly. It can also be used to discard changes in the working directory

To undo changes in the working directory and leave the index and the most recent commit unchanged:

git checkout -- . 

To undo changes in the working directory and the index, but leave the most recent commit unchanged:

git checkout HEAD -- .