The web content provides a guide on how to checkout a specific version or revision of a Git repository, illustrating the process with an example and emphasizing the utility of Git for version control.
Abstract
The article "How to Checkout Git Repo at a Specific Version or Revision" offers a step-by-step tutorial for accessing older revisions of a Git repository. It underscores the importance of version control in software development, particularly when needing to revisit past work or correct recent mistakes. The author uses their own repository as a case study, demonstrating how to locate a specific commit by date, copy the commit hash, and use Git commands to checkout the repository as it was at that point in time. The guide also explains the concept of a 'detached HEAD' state in Git and how to manage it, concluding with a reflection on the power and accessibility of Git for various professionals, not just software developers.
Opinions
The author appreciates Git's version control capabilities, noting how it eliminates the need for creating multiple versions of files.
They express the practicality of Git in real-world scenarios, such as reverting to a previous state after a mistake or exploring historical changes.
The author suggests that Git is user-friendly and beneficial beyond the software industry, advocating its use by authors, journalists, and office workers.
The article implies that understanding Git's functionality, such as the 'detached HEAD' state, can enhance user productivity and confidence in managing repositories.
How to Checkout Git Repo at a Specific Version or Revision
Step-by-Step Guide
Many times, I find myself that I need to get back to an older revision of my Git repository for reasons such as wanting to look at what I did a while ago, or somehow I messed up my current version and committed without realizing what actually I did. But hey, this is where the power of a version control software such as Git comes into the picture. At least I don’t have to create multiple files like “final.pdf”, “final-final.pdf”, etc.
How to check out for an older revision?
To checkout for an older revision, we first need to go back to back to the revision we need. This can either be found out through a date you are looking for a commit message.
Now, say we are interested in checking out this repository as it was on October 9, 2022. To do that, first, click on commits history (21 commits) as shown below:
Figure 1. By Author. Click on ’21 commits’ shown in the box above.
Now, we want to restore a revision from Oct 9, 2022. For that, let’s click on the commit hashes shown in Figure 2.
Figure 2. By Author. Click on hex, as shown in the box above to enter the commit changes of that particular day
It will show you changes that was committed on Oct 9, 2022 as shown in Figure 3, I call this diff window.
Figure 3. Diff Window showing changes that occurred during a specific commit.
From the diff window, note down the commit hash as shown in the lower box in Figure 3. You can also click on ‘Browse files’ to inspect files for this specific commit directly on your browser.
After noting down the commit hash, type the following on your computer in the command line:
git clone https://github.com/rahulbhadani/medium.com
cd medium.com
git checkout ec92a9bc7b2aa165df630ed5e268ec58fc0716a2
This will cause the local branch of your GitHub repo checkout on your commit to revert to the commit corresponding to ec92a9bc7b2aa165df630ed5e268ec58fc0716a2 .
You will get the following output in your command line terminal
Note: switching to'ec92a9bc7b2aa165df630ed5e268ec58fc0716a2'.
You are in'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) byusing -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead tofalse
HEAD is now at ec92a9b Example of Normalizing FLows
Conclusion
Version control software like Git is extremely powerful and can save hours of your time and make you more productive if you use it the right way. It is not only meant for tech-savvy professionals, nerds, and programmers, but even authors of books, journalists, and office workers can use and get benefits from it.
Art courtesy Midjourney
Follow me on LinkedIn, and Medium for more content like this.