git
branches
file viewing
version control
tutorial

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:

bash
$ git show <branch-name>:<file-path>
  • <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:

bash
$ git show feature-branch:README.md

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):

bash
$ git cat-file -p <branch-name>:<file-path>
  • -p: Pretty-print the contents.

Example:

bash
$ git cat-file -p feature-branch:README.md

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:

bash
$ git ls-tree <branch-name> <file-path>

This will output information about the file, including its blob SHA-1 object ID. With this ID, you can directly inspect the blob:

bash
$ git cat-file -p <blob-SHA>

Example:

bash
$ git ls-tree feature-branch README.md
# Find the blob SHA from the output, then:
$ git cat-file -p <obtained-blob-SHA>

Using git diff

If the purpose of viewing the file is to compare changes between the branches, git diff can be a powerful tool:

bash
$ git diff <branch-name1>..<branch-name2> -- <file-path>

This command will highlight differences between the specified branches.

Example:

To see changes made to README.md between main and feature-branch:

bash
$ git diff main..feature-branch -- README.md

Summary Table

CommandDescriptionAdditional Info
git show <branch>:<file>View file content from another branchDirectly outputs to terminal.
git cat-file -p <ref>Pretty-prints file content given a referenceRequires knowledge of the object's SHA-1.
git ls-tree <branch> <file>Lists the blob SHA-1 of a file on a branchProvides blob ID for further inspection.
git diff <branch1>..<branch2> -- <file>Shows differences in a file across branchesHighlights 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.


Course illustration
Course illustration

All Rights Reserved.