When working with a Git repository, you have two main options for retrieving code from a specific commit: cloning the repository or downloading a specific snapshot. To get the entire repository with all its commit history, use the git clone <URL> command. This will copy the complete commit history to your local device, enabling you to navigate across various commits and branches. After cloning, you can switch to a specific commit using the command git checkout <commit-hash>. This method provides full access to the repository’s history and the flexibility to make changes or navigate between commits as needed.
However, if you only need a single specific commit without the entire repository history, you can download it through the Git web interface (such as GitHub or GitLab). First, navigate to the desired commit, and then select the option to download it, often available as a ZIP file. This ZIP file will contain only a snapshot of the selected commit without any additional history or metadata. This approach is lightweight and ideal if you only need the files from that specific commit for a specific purpose without modifying or viewing the entire version history.
As a response to my struggle with cloning a repository at a specific branch and commit, as I’ve also mentioned on Stack Overflow.
As you might know, the concept of cloning in Git is different from an SVN checkout, where I can specify a particular repository revision using -r. In Git, cloning a repository includes all repository contents and defaults the HEAD to the last commit (unless the -n option is specified).
Recently, I faced a situation where I needed a specific commit from one of my repositories, as I’ve started working on a new laptop. There are two ways to do this:
- Download via GitHub / GitLab Tree View (through its branch)
- Clone the repository and checkout a specific commit
1. Download via GitHub / GitLab Tree View
If your repository host is GitHub, you can refer to the following URL format in the tree view:
https://github.com/<repo_name>/tree/<commit_sha>
Then click "Download ZIP" on the top right of the navigation bar, and you’ll get the project at the specific commit.
First, click on the commit, and select the branch.
Next, choose the “<>” (Code) button.
Before you click download, make sure the dropdown no longer points to a branch but to the tree commit ID. Once you’re certain, click download.
Alright, let’s continue tomorrow with the second case...