• Skip to main content
  • Skip to primary sidebar
  • Home
  • About
  • Subscribe BMA
  • Contact Us!

Be My Aficionado

Inspire Affection

You are here: Home / Programming / How to Fix A Blunder and Get Squashed Commits Back (Git Undo)

How to Fix A Blunder and Get Squashed Commits Back (Git Undo)5 min read

June 11, 2020 by Varun Shrivastava Leave a Comment

There will be times when you will realize “Oh, no! What’ve I done?”.

That moment is a classic moment in every developer’s life. Usually, it happens right after when you do something silly, and it costs you hours of re-work.

We tend to become careless in tasks that we do on a regular basis. And one such task for me was Git Squash.

I like my commits to be clean and understandable. Also, I have got a habit of making quick, short commits while I’m working locally. This way helps me to get back to the earlier version without losing much work. I personally recommend “Quick Commits” to everyone.

One downside of it is that you will have a lot of commits in your branch. Some of these commits are a single line change.

It is super okay when you are working alone or in a small team. But when the team size is big or there are multiple teams working on the same codebase then its really important that you maintain a clean commit history that clearly explains what is happening there.

That is why I squash the commits with a relevant message stating “What I Did” and in the description “Why I Did It” before pushing it to the master.

I frequently use Git Squash to make commits clean.

But sometimes… Things don’t go your way ๐Ÿ˜€

Git Squash

Let me explain “Git Squash” quickly for those who have not heard of it.

As the name suggests, Git Squash is basically squashing multiple commits into one.

I wrote a little example for you to follow.

So, this is how it works.

Now, that you got the idea, let’s see where I made a mistake and why I had to undo the squash.

Git Squash Undo

I was working on a project’s code for 2 days. There were a lot of small careful changes that I made. There were more than 25 commits in the code. And when I squashed the commits, I squashed them with the wrong one.

Shit Happens
Shit happens

So, there was a master commit and then there were my commits (on top of the master). But what actually happened was that I merged the commits with the master. Now, the master commit itself was changed and all my changes were in it.

Laugh all you want, but it can happen to you too.

Not only that, but I also got a lot of conflicts in the codebase. And if you are working on a large codebase, you know how hard it is to resolve the conflicts. But that was not even my concern.

The concern was, I changed the master commit. I will have to force push it to put it there. And on top of that, it would create a mess for all the team members. Because the head of the branch will no longer be in sync with the remote. And this will create conflicts for them.

So, there were only two options left –

  • I start from scratch
  • Undo SQUASH.

I would never go for the first option. It’s time-consuming, repetitive and boring. I stick with the second option.

However, there is no direct way to un-squashing the commits in Git.

UnSquashing is not even a word :p

After 30 minutes of struggle, I reached out to one of my colleague about the problem and he suggested me a life-saving command –

  • git reflog

Git Reflog to the Rescue

Whenever you make changes to your branch, git records it. Git has it into consideration already that “You will make mistake”.

So, it maintains a history of all the actions that you do inside your Git.

Reflog is a mechanism to record when the tip of branches are updated. This command is to manage the information recorded in it. Basically every action you perform inside of Git where data is stored, you can find it inside of the reflog.

When you run git reflog, this is what you see.

git reflog output
git reflog

Here you can find the list of all the commits that you made so far.

Now, all I had to do is create a new branch from the master and cherry-pick each one of the commits that I see on executing reflog command.

The current branch structure is like this.

Branch Strategy

Once I have the temp branch, I start picking each commit one-by-one (it is still better than starting from scratch, plus it takes less than 5 minutes to cherry-pick).

All I had to do is fire these commands in order.

git cherry-pick 0cbda5e
git cherry-pick c8b4401
git cherry-pick 4b3affb
git cherry-pick dd4394e
git cherry-pick cfff6b3
git cherry-pick 518699b

After cherry-picking each commit into a new branch, you can safely squash and merge the temp branch with your master. And you will not lose any data. You will be sure that all your changes are there. Just be a little more careful this time ๐Ÿ˜€

It was a relief.

Well, that’s how you save your ass ๐Ÿ˜€

Do you have a better way, please let me know in the comments below.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • More
  • Click to print (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Skype (Opens in new window)
  • Click to email this to a friend (Opens in new window)

Filed Under: Programming, Tutorials Tagged With: blunder, git, git squash, reflog, squash

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Featured Posts

A Peek inside the MNCs Development Models

March 24, 2017 By Varun Shrivastava Leave a Comment

How to become an Amazon Affiliate – A Complete Guide with Images

October 22, 2016 By Varun Shrivastava Leave a Comment

iTerm2 – Colorize The Tailed Logs In ITerm2 Terminal

August 21, 2020 By Varun Shrivastava Leave a Comment

Top 5 Best Flagship Smartphones Under 14000 INR April 2017

April 1, 2017 By Varun Shrivastava 2 Comments

Best Javascript Framework 2017

March 2, 2017 By Varun Shrivastava 1 Comment

Latest Posts

  • 3 Best Laptops (Mid-Range) For Every Use Case In India
  • Distributed System Architectural Patterns
  • The Power of being in the Present
  • Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java
  • Study Abroad Destinations : Research and Review

Categories

  • Blogging (103)
  • Cooking (11)
  • Fashion (7)
  • Finance & Money (12)
  • Programming (51)
  • Reviews (4)
  • Technology (22)
  • Travelling (4)
  • Tutorials (12)
  • Web Hosting (8)
  • Wordpress N SEO (19)

Follow us on facebook

Follow us on facebook

Grab the Deal Now!

Hostgator Starting @$3.95/mo

DigitalOcean Free Credits

Trending

Affordable Hosting amazon aoc-2020 bad luck believe in yourself best database earn money blogging education experience fashion finance Financial Freedom food friends goals google india indian cuisine indian education system java javascript life life changing love make money microservices motivation oops poor education system principles of microservices problem-solving programmer programming reality search engines seo SSD Hosting success technology tips top 5 web web developer wordpress

Copyright ยฉ 2021 ยท BeMyAficionado by Varun Shrivastava ยท WordPress

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.