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?
What is the real option here?
It's Bring it back from the dead. Please read on. 😊- Open your git repo in terminal
- Do git reflog
- 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.
- 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
*/ - 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
- Copy that and do git reset --hard {copied_HEAD_pointer} which will look something like,
git reset --hard HEAD@{4} - Tada. You’ve moved to the change you need. Just do a git push --force and you’re ready to ship the code.
Comments
Post a Comment