Not your best day. You made changes you shouldn’t have and now everything is broken… Is there a way to undo those commits?
Git keeps track of updates to the tip of branches using a mechanism called reference logs
, orreflogs
Many Git commands accept a parameter for specifying a reference or “ref”, which is a pointer to a commit. Common examples include git checkout
or git reset
.
git reflog
you can get the record of all the commits done in Git.
Now you just need to find the commit prior to the one that caused all the hassle. HEAD@{index}
represents the specified commit, so just replace the index with the correct number and run:
git reset HEAD@{index}
The default action for git reset is git reset --mixed
. It means that the changes in your working directory will be preserved but not staged (since the index was modified to match the chosen commit, the changes are not in the index anymore).
Other options are:
# Doesn’t modify the index or the working tree, leaving your changes staged for commit.
git reset --soft
# Use with caution, as it resets both the index and working tree. Uncommitted changes and all commits after will be removed.
git reset --hard
And now, you can start over from the point when everything in your repository worked like a charm. Remember to use it only locally, as modifying a shared repository is considered a serious crime.
If you found this post useful please share it with your friends 😍
Top comments (0)