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.

Advertisements

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


Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s