Joe
Joe

Reputation: 8042

How can you get one file out of a another branch of a Git repository/

I'm using Git. I have some code in one branch that I want to get. Is there an easy way to get just the version of one file from a different branch or an easy way to make this other branch appear in another directory of my file system?

Just to be clear, I want to have this other version to examine, I don't want to completely replace the version I have with it.

I'm using SourceTree, but I'd be happy to accept command line based answers as well?

Upvotes: 4

Views: 6132

Answers (2)

Nick Tomlin
Nick Tomlin

Reputation: 29271

For a quick look into a single file, git show is also an option:

git show otherbranch:./path/to/bar.foo 

This output contents of bar.foo to stdout — which you can pipe to an editor if necessary:

git show otherbranch:./path/to/bar.foo | vim -

Upvotes: 3

Chris
Chris

Reputation: 8656

I think (from the last entry in the documentation for git checkout), this will do what you're after (not sure about SourceTree though, sorry):

git checkout mybranch -- mypath/myfile

Source: https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html

git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>…

When <paths> or --patch are given, git checkout does not switch branches. It updates the named paths in the working tree from the index file or from a named <tree-ish> (most often a commit). In this case, the -b and --track options are meaningless and giving either of them results in an error. The <tree-ish> argument can be used to specify a specific tree-ish (i.e. commit, tag or tree) to update the index for the given paths before updating the working tree.

Upvotes: 5

Related Questions