Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I open a file in a branch different from the active?

Tags:

git

Say I have a branch "work" and a branch "ideas" in my git repository.

I usually work on my marvelous_file.cpp in "work" branch, but sometimes I would like to open at the same time the marvelous_file.cpp that is in my "ideas" branch.

I don't want to see what the differences between the two files, but rather take snippets of codes from the "ideas" branch and copy and past them in the file in the "work" branch.

Is this possible? If not, can I checkout the file in "ideas" into "work", but with a different name?

like image 959
lucacerone Avatar asked Oct 26 '12 16:10

lucacerone


People also ask

Does git checkout change local files?

Git will simply copy the most recently-committed version of the file to your working directory, overwriting your copy. Therefore, it's crucial that you avoid this command unless you absolutely know that you don't want your unsaved local changes.

How do I pull only a specific file in git?

git checkout origin/master -- path/to/file // git checkout <local repo name (default is origin)>/<branch name> -- path/to/file will checkout the particular file from the downloaded changes (origin/master).


1 Answers

You can use git show to do that:

git show ideas:marvelous_file.cpp

This will show it right in the Terminal. If you want that as a file, just pipe it into a file, e.g.:

git show ideas:marvelous_file.cpp > ideas.cpp
like image 59
robinst Avatar answered Sep 21 '22 23:09

robinst