DEV Community

Cover image for Take me back, please!
AbdlrahmanSaber
AbdlrahmanSaber

Posted on

Take me back, please!

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, orreflogsMany Git commands accept a parameter for specifying a reference or “ref”, which is a pointer to a commit. Common examples include git checkoutor git reset.

git reflog
Enter fullscreen mode Exit fullscreen mode

you can get the record of all the commits done in Git.

Image description

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}
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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 😍

Let’s connect on LinkedIn, Twitter

Top comments (0)