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


当前回答

已有内容的两种变体:

git archive --format=tar --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | tar -O -xf -

and:

git archive --format=zip --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | funzip

它们将文件写入标准输出。

其他回答

这对我很有用。使用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仓库:

首先,你需要克隆git存储库,并给出一个特殊提示——不签出

git clone --no-checkout <git url>

下一步是用下面的命令清除索引中的非暂存文件:

git reset

现在你可以开始拉出你想要修改的文件了:

git checkout origin/master <path to file>

现在,存储库文件夹包含您可以立即开始编辑的文件。编辑完成后,您需要执行简单而熟悉的命令序列。

git add <path to file>
git commit -m <message text>
git push

现在我们可以了!因为这是谷歌上的第一个结果,我想我应该把它更新到最新的位置。随着git 1.7.9.5的出现,我们有了git归档命令,它将允许您从远程主机检索单个文件。

git archive --remote=git://git.foo.com/project.git HEAD:path/in/repo filename | tar -x

在这里可以看到完整的答案https://stackoverflow.com/a/5324532/290784

已有内容的两种变体:

git archive --format=tar --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | tar -O -xf -

and:

git archive --format=zip --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | funzip

它们将文件写入标准输出。

我添加这个答案作为做正式签出或一些类似的本地操作的替代方案。假设您可以访问Git提供程序的web界面,您可能能够在给定的提交时直接查看任何文件。例如,在GitHub上,你可以使用这样的东西:

https://github.com/hubotio/hubot/blob/ed25584f/src/adapter.coffee

这里ed25584f是感兴趣的提交的SHA-1散列的前8个字符,后面是源文件的路径。

类似的,在Bitbucket上我们可以尝试:

https://bitbucket.org/cofarrell/stash-browse-code-plugin/src/06befe08

在本例中,我们将提交散列放在源URL的末尾。