Skip to main content

Git magic - Bring back from the dead

I had this interesting scenario where I did hard reset and force push to a commit. However I wanted one of the change from the lost commit. 

Once again, Git magic came in handy. Git never erases anything off its memory too soon. This magic spell as I call, is one of a kind and most git enthusiasts must know it by now. 

Introducing reflog

We all know what you get when you do git log. Let me anyway spell it out, git log gives you a list of changes or in other words, commits available as of the moment in that local repository in the chronologically descending order. 

Similarly, git reflog gives you a list if changes or in other words, change history that happened in that local repository in chronologically descending order. For instance, if you have amended the same commit 5 times, you can choose to go back to the 3rd commit amend and restart your work from there.

This situation is what I call as, 

Bring back from the dead

Let us consider the following scenario,

  • You worked and pushed a change
  • Customer comes in and says that they don’t want the change and so you git reset hard to the previous commit
  • Now after a day, customer comes back and says they want that change back again

What options do you have?

  • Try and bring back the changes from your mind?
  • Look for the closed PR changes and re-do them manually from there?
  • or worse, scold your customer inside yourself and do the first point?
I’ve been at that stages. Some of my backend buddies used to use their IDE’s local cache to get them back. All this while me, a lame Sublime text user has been under the impression it’s some magic that IntelliJ does that sublime cannot. Little I knew it was all Git underneath. 

What is the real option here?

It's Bring it back from the dead. Please read on. 😊
  1. Open your git repo in terminal
  2. Do git reflog
  3. This will show a huge list of historical changes. When you look at this, you’ll know the steps you did to get your repo to where it is now. git log is like calculating displacement and git reflog is like finding the distance covered, what all stops you made.
  4. A typical reflog will look like,

    ef9080e HEAD@{1}: commit (amend): build: add changes for success
    gh0191f HEAD@{2}: commit (amend): build: add changes for success
    hi1202g HEAD@{3}: commit (amend): build: add changes for success
    f1be775 HEAD@{4}: reset: moving to HEAD@{1}
    e3g351f HEAD@{5}: reset: moving to HEAD@{13}
    6b63233 HEAD@{6}: rebase (finish): returning to refs/heads/build-failing-issue
    6b63233 HEAD@{7}: rebase (pick): build: testing build fail
    2224786 (tag: v1.9.0, upstream/master, origin/master, origin/HEAD, master) HEAD@{8}: rebase (start): checkout upstream/master

    You could know what change happened at that Head pointer by reading the type as highlighted in the orange and the change message in black.

    /**
     *
    @disclaimer
     * The commit hash and messages used are not real and wrote for demo purposes
    */
  5. Here choose a change you want to go back to. Unlike the commit hash, you’ll have to choose a head pointer. Something like HEAD@{1} or HEAD@{10} or HEAD@{15} or it could go on
  6. Copy that and do git reset --hard {copied_HEAD_pointer} which will look something like,

       
    git reset --hard HEAD@{4}

  7. Tada. You’ve moved to the change you need. Just do a git push --force and you’re ready to ship the code. 
I hope this is helpful for folks who like terminal a lot more than IDEs and for people who were curious to know the tech underneath the IDEs

Comments

Popular posts from this blog

WebRTC - What the heck?

Over the past few weeks, I happened to work on stuff that enabled me to understand what WebRTC is and how useful it is.  The full form? Web R eal- T ime C ommunication The history It's first release was by 2011. If you want to know more, well, please read the wikipedia . WebRTC  has been a boon to web developers who want to build streaming applications or video calling applications. As you move downward, you'll just may be understand how WebRTC works but nothing technicals.  The story Let's begin with a short story. Long long ago, so long ago, nobody knew how long ago, there lived two shop keeper farmers John & Finch. It was that old point in time when barter system was a thing and money wasn't invented.  These shopkeepers lived in different cities across a river and the cities were connected by a rock solid bridge. Like how the golden gate connects the San Francisco city and the Marin County. Finch is a very private person and takes hard time to trust people. John

TDD - How to go about it? - A quick start example

So, it's only recently I've been doing TDD. I have written unit tests before but those were all tests after writing the actual code. Confusing statement right? /**  * @disclaimer  * I'm just preaching what I practice and some parts of it could be wrong.  * Please feel free to leave comments if am wrong  * And I would be happy to stand corrected */ What is TDD? TDD, expanded as Test Driven Development could have this statement as one of the definitions. "TDD is nothing but the art of writing down the business use case one by one and write code based on it paralelly" It would feel like why do I need to write test for that? We could do the same with pen & paper and write codes. But then, you again circle back to write tests for the same. So why not do it this way?  Therefore, let's start with an example, Write a method that accepts two positive numbers as input and returns their sum Let's try and do a mock TDD for the above problem.  Case 1 - Function add

Git magic - Make commits disappear

So fellas, it's been a while since I wrote and I hope this article is of help to whoever reads this.  Let's start with bit of an introduction. Until my previous employment, there wasn't much guideline on how we write Github's commit message. One single feature could have n number of commits and it doesn't bother the repository.  Now, am part of  @webex 's  webex-js-sdk  team where committing guidelines are very strict and having a bit more deep knowledge of git commands has become essential. This made me explore git commands on regular basis. So I thought why keep them to myself while I can share them. It was my first ever PR and my changes were approved except for a few minor changes. I honestly didn't know how to make changes to existing commit and now, I had two choices.  Create a new branch from master and re-do the work manually Do some git magic and make the current branch work well.  If it were old times, I'd have gone with option 1. It's very