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?
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.
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 -
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With