Take me back, please! - Git tricks (1)

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

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.

Note

I already created a big article have 11 tricks in git and this trick one of them, if you’d like to see it, check this link

If you found this post useful please share it with your friends and follow me😍

This will help me a lot 🙏

Let’s connect on LinkedIn, Twitter

--

--

--

Software engineer, Problem solver, Geek.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why UML Diagrams are Important & Easy?

My iOS 14 Home Screen Set Up

#47 YAGNI

Are you Coding and Don’t Even Know it?

Take Jaeger for a HotROD ride

LastChance Ambassador Program 💵

Expert Tips before Hire PHP developers

Best Practices for Migrating Virtual Machines to Azure

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abdlrahmansaber

Abdlrahmansaber

Software engineer, Problem solver, Geek.

More from Medium

How view branches sorted by date in specific format and order - Git tricks(2)

My GitHub Diary.

My Most Used Git Commands

Git for Beginners Part One — Intro