Git

How to Undo the Most Recent Local Commits in Git

February 28, 2023

This guide is part of the "Snippets" series. This series is focused on providing simple and accessible tutorials on various topics relating to development!


To undo the most recent local commits in Git, you can use the git reset command with the --hard option followed by the number of commits you want to undo.

For example, to undo the last commit, you can run:

git reset --hard HEAD~1

This will remove the most recent commit from your local repository along with any changes made in that commit. Note that this is a destructive operation and you will lose any changes that were made in the undone commits.

If you want to keep the changes made in the undone commits, you can use the git reset command with the --soft option instead. This will remove the commit from your local repository, but keep the changes staged so that you can commit them again.

git reset --soft HEAD~1

If you have pushed the commits to a remote repository, you will need to use git push with the --force option to update the remote repository with the changes.

git push --force origin <branch-name>

External resources:







You might also like

The latest from the blog