我如何从一个git回购签出一个文件?


当前回答

Git checkout <other-branch>—<single-file>适用于我的Git .2.37.1。 然而,该文件是(git-魔术)提交的,我不能看到正确的git diff。 然后运行git restore——staging db/structure。SQL来取消它。

这样我就有了我想要的确切版本的文件,我可以看到该文件与其他版本的区别。

其他回答

说文件名是123.txt,这对我来说是有效的:

git checkout --theirs  123.txt

如果文件在目录a中,请确保正确地指定它:

git checkout --theirs  "A/123.txt"

如果您编辑了文件的本地版本,并希望恢复到中央服务器上维护的原始版本,可以使用Git扩展轻松实现。

最初文件将被标记为提交,因为它已被修改 在文件树菜单中选择(双击)该文件 列出了单个文件的修订树。 选择树的顶部/HEAD,然后右键单击save as 保存该文件以覆盖修改后的文件本地版本 文件现在有正确的版本,将不再被标记为提交!

简单!

最小的指南

Git checkout——<filename>


裁判:https://git-scm.com/docs/git-checkout

Dup:在Git中撤销一个文件的工作副本修改?

非常简单:

git checkout from-branch-name -- path/to/the/file/you/want

这将不会签出from-branch-name分支。您将停留在您所在的分支上,并且只有该文件将从指定的分支签出。

下面是git-checkout手册的相关部分

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.

向Ariejan de Vroom致敬,他在这篇博客文章中教会了我这些。

Git checkout <other-branch>—<single-file>适用于我的Git .2.37.1。 然而,该文件是(git-魔术)提交的,我不能看到正确的git diff。 然后运行git restore——staging db/structure。SQL来取消它。

这样我就有了我想要的确切版本的文件,我可以看到该文件与其他版本的区别。