Git
Version Control
File Retrieval
Git Commands
Programming

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.

Browse interview questions

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~2 etc., 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:

bash
git checkout <revision> -- <path/to/file>

Example:

Suppose you want to retrieve the file example.txt from the commit with the hash 9fceb02. You would use the following command:

bash
git checkout 9fceb02 -- example.txt

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:

bash
git restore --source <revision> <path/to/file>

Example:

To retrieve the example.txt from the commit 9fceb02 using restore, you would run:

bash
git restore --source 9fceb02 example.txt

This will restore example.txt to your working directory, exactly as it was in the specified commit.

Table: Command Comparison

CommandUsageGit Version Requirement
checkoutgit checkout <revision> -- <file>Any version
restoregit restore --source <revision> <file>2.23 and later

Additional Considerations

  • Impact on Local Changes: Both the checkout and restore commands 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 show command:
bash
  git show <revision>:<path/to/file>

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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.