<tutorialjinni.com/>

Git Revert Last Commit

Posted Under: Git, Tutorials on Aug 8, 2023
To undo the most recent local commits in Git, you can use the git reset command. There are two common ways to do this depending on your desired outcome:

1) To keep the changes from the undone commits as unstaged modifications:

git reset HEAD~1
This command will remove the most recent commit from the branch and move the branch pointer back one commit. The changes from the undone commit will still be present in your working directory as unstaged modifications, allowing you to make further adjustments before committing again.

2) To completely discard the changes from the undone commits:

git reset --hard HEAD~1
This command will remove the most recent commit from the branch and move the branch pointer back one commit. Additionally, it will discard all the changes from the undone commit, reverting your working directory to the state of the commit before the one you just removed. Be cautious with this option, as it will permanently discard any changes made in the undone commit. After using either of these commands, if you want to push these changes to a remote repository, you will need to force-push since the branch's history has changed. This can be done using:
git push -f origin 
Note: Make sure to replace with the actual name of the branch you are working on. Also, be cautious when using git reset --hard, as it discards changes permanently, and they won't be recoverable unless you have a backup or a way to recover the discarded commits. Always double-check your changes before performing such actions.


imgae