David Y.
—How do I undo the most recent local commits in Git?
The git reset
command will return the current branch to a specified previous commit. By default, this command will remove commits from the current branch’s history while leaving the files in the working tree untouched. This allows you to redo one or more commits without losing any work.
When calling git reset
, you need to specify the commit to reset to. You can get the hash of the commit you want from git log
, or you can specify an ancestor of HEAD
, the current commit, using the tilde (~) suffix. The following commands will undo and redo the most recent commit:
git add . git commit -m "This commit is a mistake" git reset HEAD~ git add main.py # need to re-add files after reset git commit -m "This commit corrects the mistake"
To undo the last two commits, use the commands:
git add . git commit -m "This commit is a mistake" # make changes git add . git commit -m "This commit is another mistake" git reset HEAD~2 git add . git commit -m "this commit corrects both mistakes"
If you don’t want to have to re-stage your files after a reset, you can use the --soft
flag:
git add . git commit -m "This commit is a mistake" git reset --soft HEAD~ # no need to git add, as files are already staged git commit -m "This commit corrects the mistake"
If you want to reset both the Git history and the working tree to the state of a previous commit, you can use the --hard
flag. Note that this will reverse all changes made to tracked files, including ones that haven’t yet been committed.
git add . git commit -m "" git reset --hard HEAD~
git reset --hard
should be used with caution. However, you can still retrieve any deleted commits using git reflog
, up to 90 days after they were deleted. When run, git reflog
will show a list of commits previously on the tips of branches. From this list, you can pick out the partial hash of the commit (e.g. 5c8f5a7
) to restore and create a new branch for it:
git checkout -b restored-commit-branch 5c8f5a7
If you would like to preserve your repository’s history but return the files to a previous state, you can use git revert
to create new commits that do the opposite of existing commits, i.e. removing lines and files that were added and adding lines and files that were removed:
git add . git commit -m "This commit is a mistake" git revert HEAD # will create a new commit doing the opposite of the one above
For more on git revert
, see our answer for reverting to a previous commit.
If you’re looking to get a deeper understanding of how web performance optimization works, take a look at the following articles:
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.