Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 392
Joe Avatar asked Feb 15 '23 10:02

Joe


2 Answers

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.

like image 68
Chris Avatar answered Feb 18 '23 00:02

Chris


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 -
like image 27
Nick Tomlin Avatar answered Feb 17 '23 23:02

Nick Tomlin