Git
File Retrieval
Version Control
Git Revision
Git Best Practices

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

Retrieving a single file from a specific revision in Git is a useful skill when dealing with version control and can be particularly valuable during bug fixes or when recovering mistakenly modified or deleted code. Below, we will explore the step-by-step process and associated commands to perform this task efficiently.

Basic Concepts

Before diving into the procedure, let's clarify some Git concepts related to this task:

  • Commit: A snapshot of changes in your project. Identified by a unique SHA-1 hash.
  • Revision: Refers to a specific state of the repository, which can be represented by a commit hash, branch name, or tag.
  • Working Directory: The area where files are checked out and can be modified.
  • Staging Area (Index): The area where changes are prepped for the next commit.

Steps to Retrieve a Single File from a Specific Revision

1. Identify the Commit Hash

To retrieve a file, you must first know the commit hash where the desired version of the file resides. This can be found using:

bash
git log

This command provides a list of commits along with their messages, author, and commit hash. Identify the commit that contains the desired version of the file.

2. Extract the File

Once you have the commit hash, use the git checkout command to retrieve the file:

bash
git checkout <commit-hash> -- <path-to-file>
  • <commit-hash>: The specific commit hash that contains the file version you need.
  • <path-to-file>: The relative path to the file within the repository.

Example

Suppose you want to retrieve a file named example.txt from commit abc1234. You would perform the following:

bash
git checkout abc1234 -- example.txt

This command checks out example.txt from the specified commit into your working directory, overwriting any existing version of the file in your current branch.

3. Confirm the Change

After performing the checkout, it is wise to review the file to ensure it has been retrieved correctly.

bash
cat example.txt

This command displays the content of example.txt so you can verify its correctness.

Additional Details

Conflict Resolution

If the file in your working directory has uncommitted changes, Git will issue a warning about potential data loss. In such cases, it's crucial to either stash or commit these changes before proceeding with the checkout.

Stashing Changes

bash
git stash

This command temporarily saves your work without committing so you can retrieve the file without losing progress.

Revert Unwanted Changes After Checkout

If you wish to revert back to your original file after checking out, use:

bash
git checkout HEAD -- <path-to-file>

This command reverts the file to its state at the last commit in your current branch.

Summary Table

TaskCommand ExampleNotes
Identify commit hashgit logReview commit history to find hash associated with desired changes.
Checkout file from specific revisiongit checkout abc1234 -- example.txtReplace abc1234 with actual commit hash and example.txt with actual path.
Verify file contentcat example.txtEnsure the file content is as expected.
Stash uncommitted changesgit stashUse before checkout if changes are pending.
Revert file to original stategit checkout HEAD -- example.txtReverts to last committed state in current branch.

Conclusion

Git provides powerful tools for managing file versions throughout its history, and understanding how to retrieve files from specific revisions can significantly enhance your version control capabilities. By identifying the commit hash, using the checkout command effectively, and managing uncommitted changes, you can adeptly manage and rectify changes in your repository.


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