How to retrieve a single file from a specific revision in Git?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When working with the Git version control system, you may occasionally need to retrieve a single file from a specific revision. This can be useful in various scenarios, such as when you need to examine or restore an earlier version of a file without affecting the rest of your project. Here’s a step-by-step guide on how to achieve this, along with some technical explanations and examples.
Understanding Git Revisions
In Git, every commit is identified by a unique SHA-1 hash. This hash works as an identifier for that particular snapshot of your repository. Revisions in Git can be specified in several ways:
- A specific commit hash
- Tags pointing to certain commits
- Relative references, such as
HEAD,HEAD~1,HEAD~2etc., which refer to the current and previous commits respectively
Retrieving a Single File from a Specific Revision
To retrieve a single file from a specific revision in Git, you can use the checkout or restore (introduced in Git 2.23) command. Let's break down each approach:
Using Git Checkout
The checkout command can be used to check out specific files from specific revisions. Here’s the syntax you would use:
Example:
Suppose you want to retrieve the file example.txt from the commit with the hash 9fceb02. You would use the following command:
This command tells Git to get the example.txt file as it appeared in the commit 9fceb02 and place it in your working directory.
Using Git Restore
For newer versions of Git, the restore command is preferred for manipulating the working tree directly. It is more explicit and versatile. Here’s how you use it:
Example:
To retrieve the example.txt from the commit 9fceb02 using restore, you would run:
This will restore example.txt to your working directory, exactly as it was in the specified commit.
Table: Command Comparison
| Command | Usage | Git Version Requirement |
checkout | git checkout <revision> -- <file> | Any version |
restore | git restore --source <revision> <file> | 2.23 and later |
Additional Considerations
- Impact on Local Changes: Both the
checkoutandrestorecommands will overwrite the local version of the file if it exists. Be sure to commit any local changes before you run these commands to avoid losing work. - Nondestructive Alternative: If you prefer to view the contents of the file without changing your working directory, use the
showcommand:
This command displays the content of the file in the terminal, where <revision> can be a commit hash or any other type of revision reference, and <path/to/file> should be the path to the file in the repository.
By understanding these commands and how to apply them, you will be able to manage and navigate your Git repositories more effectively. Whether you need to restore a previous version of a file or simply inspect an old state, these tools provide powerful ways to leverage Git's capabilities.
Related reading
- How to retrieve a single file from a specific revision in Git?
- How to return from 'detached HEAD' state?
- How to reverse apply a stash?
- How to revert a git rm -r .?
- How to revert initial git commit?
- How to revert to origin's master branch's version of file
- How to search a Git repository by commit message?
- How to search a Git repository by commit message?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.