Git: Time Travelling and Undoing Changes

Git: Time Travelling and Undoing Changes

Table of contents

No heading

No headings in the article.

Last week, I had my very first Git-induced panic attack - the ‘detached HEAD’ message had made its first appearance on my terminal. What ensued was a catastrophically gone wrong resolution of a merge conflict. That was when the real power of Git shone through. Because Git kept track of my work AND my, ahem, mistakes, I could time travel back and fetch a version of my data to work on.

image.png

That leads me to two nuggets of wisdom I share with any and everybody I meet, who’s on the same path of self-discovery as me:

  1. Make mistakes. As many and as early as you can.
  2. The best time to learn Git is yesterday.

In this article, we will discuss why and how we can go back in time and check out how our previous commits aka how our work looked like, be it earlier today, yesterday, or even 1 month ago. And we can do this using only 3 simple git commands. But first, let us understand what is HEAD in Git.

In Git, the HEAD is a reference that points to the tip (latest commit) of a branch.

A detached HEAD would simply mean, a HEAD that’s not pointing to the tip of the branch it's on, but rather at some other commit on the branch’s history. (Post on Git branches coming up soon!)

Time to go time travelling in Git! Let’s go!

Scenario 1:

You work on a new feature of your website, implementing a navbar for example. It works great and you’re happy. Its the end of the day and you’re leaving work, you commit the navbar code and clock out for the day.

The next day, you come back, add a couple of cool features to the navbar and commit those. Suddenly you realize some of the links on the navbar aren't functioning properly. They did yesterday, though, right? You search and search, but can't seem to find the bug. You think to yourself “if only I could go back to how my code looked yesterday and try to figure out a solution from that.”

Then you remember about the power of Git! You open Git’s book of magical spells and find the correct incantation for that. It goes:

git checkout <commit-id>

You type git log , find the specific commit you’re looking for and enter the magical incantation with it, and VOILA! You looking at yesterday’s work as you left it before leaving work. After you’ve had a good look around and want to return to what you’re working on currently, you use the git incantation: git switch main (newer way of switching branches) or git checkout main (slightly older way of doing the same. Both work perfectly alright in this case. Easy Peasy!

1.png

Scenario 2:

You decide you like the code from yesterday and want to just continue working with that. You can do this by making a new branch at that commit and the magical spell to do that is git switch -c <new-branch-name>. Now you can switch between your main branch (which holds today’s work) and the (which holds yesterday's work) easily.

Scenario 3:

Alright! You’ve gone back in history and are working on an older commit in a new branch. It’s 5pm again, you decide to commit your latest work (remember, atomic commits are best!) and continue the next day.

Early next day you are struck with inspiration and rush to work to continue woking on your navbar. Only, 1 hour into work and after you’ve had your first cup of coffee, you realize your inspiration wasn't so amazing after all and want to start work on a clean slate. Since you haven't committed anything yet (it is after all only 1 hour into work till now), you open Git’s magical spell book and quickly look up the spell to ‘restore’ your work from 1 hour ago: git restore <filename>. This lets you revert ‘uncommitted’ changes in your working copy to the last made commit in history, i.e. yesterday evening’s work.

Woohoo! You’re starting to get a hang of this Git thing already! Thank God for the magical book of spells (aka documentation), though. Time to put what we’ve learned to practice now. Look at the comic strip below and try to guess which command would help HEAD get back to main safely. Best of Luck!

2.png