在不更改分支的情况下查看不同 Git 分支中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7856416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
View a file in a different Git branch without changing branches
提问by Schneems
Is it possible to open a file in a git branch without checking out that branch? How?
是否可以在不检查该分支的情况下在 git 分支中打开文件?如何?
Essentially I want to be able to open a file in my github pagesbranch without switching branches all the time. I don't want to modify it, just want to view it.
本质上,我希望能够在我的github pages分支中打开一个文件,而无需一直切换分支。我不想修改它,只想查看它。
回答by Scolytus
This should work:
这应该有效:
git show branch:file
Where branchcan be any ref (branch, tag, HEAD, ...) and fileis the full path of the file. To export it you could use
其中branch可以是任何引用(分支、标签、HEAD 等),而file是文件的完整路径。要导出它,您可以使用
git show branch:file > exported_file
You should also look at VonC's answers to some related questions:
您还应该查看VonC对一些相关问题的回答:
- How to retrieve a single file from specific revision in Git?
- How to get just one file from another branch
UPDATE 2015-01-19:
更新 2015-01-19:
Nowadays you can use relative paths with git show a1b35:./file.txt
.
如今,您可以将相对路径与git show a1b35:./file.txt
.
回答by Adam Dymitruk
git show somebranch:path/to/your/file
you can also do multiple files and have them concatenated:
您还可以创建多个文件并将它们连接起来:
git show branchA~10:fileA branchB^^:fileB
You do not have to provide the full path to the file, relative paths are acceptable e.g.:
您不必提供文件的完整路径,可以接受相对路径,例如:
git show branchA~10:../src/hello.c
If you want to get the file in the local directory (revert just one file) you can checkout:
如果您想获取本地目录中的文件(仅还原一个文件),您可以签出:
git checkout somebranch^^^ -- path/to/file
回答by inger
A simple, newbie friendly way for looking into a file:
git gui browser <branch>
which lets you explore the contents of any file.
查看文件的简单、新手友好的方式:
git gui browser <branch>
它可以让您浏览任何文件的内容。
It's also there in the File menu of git gui
. Most other -more advanced- GUI wrappers (Qgit, Egit, etc..) offer browsing/opening files as well.
它也在git gui
. 大多数其他更高级的 GUI 包装器(Qgit、Egit 等)也提供浏览/打开文件的功能。
回答by legoscia
If you're using Emacs, you can type C-x v ~
to see a different revision of the file you're currently editing (tags, branches and hashes all work).
如果您使用的是 Emacs,您可以键入C-x v ~
以查看您当前正在编辑的文件的不同版本(标签、分支和散列都有效)。
回答by akuhn
Add the following to your ~/.gitconfig
file
将以下内容添加到您的~/.gitconfig
文件中
[alias]
cat = "!git show \":\" #"
And then try this
然后试试这个
git cat BRANCHNAME FILEPATH
Personally I prefer separate parameters without a colon. Why? This choice mirrors the parameters of the checkout
command, which I tend to use rather frequently and I find it thus much easier to remember than the bizarro colon-separated parameter of the show
command.
我个人更喜欢没有冒号的单独参数。为什么?这个选择反映了checkout
命令的参数,我倾向于经常使用它,因此我发现它比命令的奇怪的冒号分隔参数更容易记住show
。