14

On the server I have bare repository which is origin for development process and to simplify deployment to QA environment.

So in post-receive it simply does

GIT_WORK_TREE=/home/dev git checkout -f

But as product gets more complicated there are some other things should be happening. So now it is handled by deploy.sh script which is also tracked by repository. So what I want to do is to be able instead of checking out whole repository is to checkout only deploy.sh and run it. I thought something like that would work:

SOURCE_PATH="/home/dev"
GIT_WORK_TREE=$SOURCE_PATH git checkout deploy.sh
$SOURCE_PATH"/deploy.sh"

But it does not work giving error:

error: pathspec 'deploy.sh' did not match any file(s) known to git.

What am I doing wrong? Or is it just impossible to do this way?

4

3 回答 3

16

正如我在“只从 git 检出一个文件”中解释的那样,如果不先克隆或获取,就不能只检出一个文件。

但是你git show是那个文件,这意味着你可以将它的内容转储到一个/another/path./deploy.sh文件中,然后执行那个文件。

git-show HEAD:full/repo/path/to/deploy.sh > /another/path./deploy.sh
/another/path./deploy.sh

由于您从 post-receive 挂钩执行该操作,因此git-show将显示deploy.sh文件的最新版本。


另一种选择是尝试

 GIT_WORK_TREE=$SOURCE_PATH git checkout -- path/to/deploy.sh

并且只签出该文件,直接在您的工作树中。

' --' 帮助 git 命令理解它是一个文件,而不是另一个参数,如标签或命名分支。

OP AlexKey的测试来看,它要求工作树至少已被(完全)检出一次。

于 2012-11-28T10:35:25.773 回答
3

我知道这是ooooooooold,但我找到了我自己的这个功能的用例,并在将一些解决方案组合成一个简单的单行代码之前环顾了一段时间以寻找更好的解决方案:

GIT_WORK_TREE=/home/dev git checkout $branch -- deploy.sh

就我而言,我只是希望能够“窥视”我的一些裸存储库,而无需打开整个存储库(其中一些很大)。人们在谈论稀疏结账和其他类似的事情,但我只需要一次性功能。例如,要查看“文档/健康记录”文件夹,我会执行以下操作:

GIT_WORK_TREE=/tmp/my-files git checkout master -- "Documents/Health Records"

瞧!它确实出现了。

于 2018-03-05T03:48:45.933 回答
2

这种git show或类似的 git cat-file blob方法对于文本文件或多或少都可以正常工作,但对于二进制文件却没有希望。

更好的方法可以可靠地用于任何类型的文件,甚至可以检查整个文件夹:

git archive mybranch folder/file.txt --output result.tar

它创建一个包含所需内容的 tar 存档,正是位于源代码控制中的文件。与二进制文件完美配合。

您唯一需要做的就是提取此 tar 文件

tar -xf result.tar

有关更多详细信息,请参阅我的博文

于 2013-02-27T21:48:28.167 回答