Better Git: Interact With a Branch, Without Checking it Out.Posted: April 10, 2013 Filed under: Better Git 2 Comments
Tricks for interacting with a branch, without checking it out.
Browse a directory (like ls):
git show [ref]:[path]
> git show master:your/path/
See contents of a file (command as above):
git show [ref]:[filepath]
> git show master:your/path/file.php
Checkout a specific file or directory from a different branch:
git checkout [ref] -- [path]
> git checkout master -- your/path/file.php
Note: There are other ways to do similar tasks such as `git ls-tree` and they may have more options. However I find these to be more accessible and easy to remember.
With certain text editors (those that can be opened by the command line – hint hint sublime nudge nudge), you can expand upon the git show [ref]:[filepath] command above, and actually pipe it into your editor. For example:
git show some-branch:path/to/file | subl
I even made it a neat little alias within my bash_profile:
git show $1:$2 | subl
git show $1:$2 | subl &
The & will free up your terminal while the sublime tab is open. Thanks for the tip Jason!