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


当前回答

这对我很有用。使用git和一些shell命令

git clone --no-checkout --depth 1 git.example.com/project.git && cd project && git show HEAD:path/to/file_you_need > ../file_you_need && cd .. && rm -rf project

其他回答

非常简单:

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致敬,他在这篇博客文章中教会了我这些。

首先使用-n选项和——depth 1选项克隆repo,前者抑制所有文件的默认签出,后者意味着它只获取每个文件的最新修订

git clone -n git://path/to/the_repo.git --depth 1

然后签出你想要的文件,就像这样:

cd the_repo
git checkout HEAD name_of_file

如果你有一个本地修改的文件(搅乱了git的文件),只需要这样做:

git checkout origin/master filename

Git签出-切换分支或恢复工作树文件,(这里我们什么都不切换,只是覆盖文件 Origin /master -您当前的分支,或者您可以使用特定的修订号,例如:cd0fa799c582e94e59e5b21e872f5ffe2ad0154b, 包含项目主目录路径的文件名(.git目录所在位置) 所以如果你有结构:

“.git

公共/ index . html

public / css / style . css

供应商

composer.lock”

想要重新加载index.html -只需使用public/index.html

在git中,你在更新文件之前不会“签出”它们-这似乎是你所追求的。

许多系统,如clearcase, csv等,都要求您在对文件进行更改之前“签出”文件。Git不需要这个。克隆存储库,然后在存储库的本地副本中进行更改。

一旦你更新了文件,你可以做:

git status

查看修改了哪些文件。你先添加你想要提交给index的对象(index就像一个要签入的列表):

git add .

or

git add blah.c

然后do git status会告诉你哪些文件被修改了,哪些文件在索引中准备提交或签入。

要将文件提交到存储库副本,请执行以下操作:

git commit -a -m "commit message here"

参见git网站上的手册和指南链接。

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

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