View a file in a different Git branch without changing branches
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In a Git workflow, there are times when you may need to view or analyze the contents of a file from a different branch without having to actually switch from your current branch. This can be incredibly useful for comparing changes, reviewing past states, or validating modifications without interrupting your current branch workflow. This article will cover the methods and commands to achieve this, backed with explanations, examples, and a summary table for quick reference.
Accessing a File from Another Branch
Using git show
The git show command allows you to view the contents of a file on a different branch. Here's the syntax:
<branch-name>: The name of the branch you want to view the file from.<file-path>: The path of the file in the repository.
Example:
Suppose you want to view the contents of README.md from a branch named feature-branch:
This command will output the file’s contents to your terminal. It’s a quick way to peek into a file without altering your current branch context.
Using git cat-file
Similar to git show, git cat-file can be used to achieve the same goal but with more flexibility in how you reference objects (like blobs, trees, and commits):
-p: Pretty-print the contents.
Example:
The command above will display the contents of README.md from feature-branch.
Direct Path to the Blob Object
In some cases, you might want to access a blob object directly. First, you need to find the object ID using:
This will output information about the file, including its blob SHA-1 object ID. With this ID, you can directly inspect the blob:
Example:
Using git diff
If the purpose of viewing the file is to compare changes between the branches, git diff can be a powerful tool:
This command will highlight differences between the specified branches.
Example:
To see changes made to README.md between main and feature-branch:
Summary Table
| Command | Description | Additional Info |
git show <branch>:<file> | View file content from another branch | Directly outputs to terminal. |
git cat-file -p <ref> | Pretty-prints file content given a reference | Requires knowledge of the object's SHA-1. |
git ls-tree <branch> <file> | Lists the blob SHA-1 of a file on a branch | Provides blob ID for further inspection. |
git diff <branch1>..<branch2> -- <file> | Shows differences in a file across branches | Highlights modifications line-by-line. |
Conclusion
Being able to view a file from a different Git branch without switching branches is a useful skill that optimizes your workflow, especially when managing multiple features or fixes concurrently. These Git commands offer versatile ways to access and inspect files, enhancing your development process while keeping your current branch's context intact. Choose the method best suited to your needs, whether it’s for ad-hoc analysis or comprehensive comparison.

