• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

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

June 11, 2020 by varunshrivastava 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.

Related

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

Primary Sidebar

Subscribe to Blog via Email

Do you enjoy the content? Feel free to leave your email with me to receive new content straight to your inbox. I'm an engineer, you can trust me :)

Join 874 other subscribers

Latest Podcasts

Recent Posts

  • Is The Cosmos a Vast Computation?
  • Building Semantic Search for E-commerce Using Product Embeddings and OpenSearch
  • Leader Election with ZooKeeper: Simplifying Distributed Systems Management
  • AWS Serverless Event Driven Data Ingestion from Multiple and Diverse Sources
  • A Step-by-Step Guide to Deploy a Static Website with CloudFront and S3 Using CDK Behind A Custom Domain

Recent Comments

  • Varun Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Vaibhav Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Varun Shrivastava on Should Girls Wear Short Clothes?
  • D on Should Girls Wear Short Clothes?
  • disqus_X5PikVsRAg on Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java

Categories

  • Blogging
  • Cooking
  • Fashion
  • Finance & Money
  • Programming
  • Reviews
  • Software Quality Assurance
  • Technology
  • Travelling
  • Tutorials
  • Web Hosting
  • Wordpress N SEO

Archives

  • November 2024
  • September 2024
  • July 2024
  • April 2024
  • February 2024
  • November 2023
  • June 2023
  • May 2023
  • April 2023
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • November 2021
  • September 2021
  • August 2021
  • June 2021
  • May 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016

Tags

Affordable Hosting (4) algorithms (4) amazon (3) aoc-2020 (7) believe in yourself (4) best (4) database (4) earn money blogging (5) education (4) elementary sorting algorithms (4) experience (3) fashion (4) finance (6) Financial Freedom (7) food (7) friends (3) goals (5) google (5) india (10) indian cuisine (5) indian education system (4) java (16) life (16) life changing (4) love (4) make money (3) microservices (9) motivation (4) oops (4) podcast (6) poor education system (4) principles of microservices (5) problem-solving (7) programmer (5) programming (28) python (5) reality (3) seo (6) spring (3) success (10) success factor (4) technology (4) top 5 (7) typescript (3) wordpress (7)

Copyright © 2025 · Be My Aficionado · WordPress · Log in

Go to mobile version