Retrieving a single file from a specific revision in Git can be achieved using the git show command. Specifically, you can specify the revision identifier (such as a commit hash or branch name) along with the file path to view or retrieve the specific version of the file. Below, I will detail the step-by-step process and provide an example.
Step-by-Step Process
- Determine the file path and the revision to inspect Ensure you know the full path of the file and the revision you want to inspect (e.g., commit hash, branch name, or tag name).
- Use the git show command The command format is:
git show <revision>:<file-path>where<revision>is the identifier for the revision (e.g., a commit hash), and<file-path>is the path to the file in the repository. - View or save the output After executing the command, the terminal will display the content of the file at the specified revision. If you need to save this content to a new file, you can redirect the output to a file.
Example
Assume we have a commit hash abc1234, and we want to view the file named example.txt in this commit, which is located in the project's root directory.
View command:
bashgit show abc1234:example.txt
This command will display the content of the example.txt file at the abc1234 commit revision in the terminal.
Save to a new file:
bashgit show abc1234:example.txt > old_example.txt
This command will save the content of the example.txt file at the abc1234 commit revision to the old_example.txt file in the current directory.
This concludes the method for retrieving a single file from a specific revision in Git.