Better Git: Interact With a Branch, Without Checking it Out.

Tricks for interacting with a branch, without checking it out.

Browse a directory (like ls):
Syntax: git show [ref]:[path]

> git show master:your/path/

See contents of a file (command as above):
Syntax: git show [ref]:[filepath]

> git show master:your/path/file.php

Checkout a specific file or directory from a different branch:
Syntax: 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.

2 Comments on “Better Git: Interact With a Branch, Without Checking it Out.”

  1. 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:

    externalBranchSubl() {
    git show $1:$2 | subl
    alias gsubl=externalBranchSubl

    • eddiemoya says:

      externalBranchSubl() {
      git show $1:$2 | subl &

      The & will free up your terminal while the sublime tab is open. Thanks for the tip Jason!

Leave a Reply to eddiemoya Cancel reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s