且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

git - 在裸仓库中检出单个文件

更新时间:2023-02-14 23:15:44

正如我在仅从git签出一个文件,则无法首先克隆或提取一个文件即无法签出一个文件。



但是你该文件,这意味着您可以将其内容转储到 /another/path./中

  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 的测试中,它要求工作树至少被检出(完全)一次。

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?

As I explain in "checkout only one file from git", you cannot checkout just one file without cloning or fetching first.

But you git show that file, which means you can dump its content into a /another/path./deploy.sh file, and execute that file.

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

Since you execute that from a post-receive hook, the git-show will show the latest version of the deploy.sh file.


The other alternative would be try

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

And checkout only that file, directly in your working tree.

The '--' help the git command to understand it is a file, not another parameter like a tag or a named branch.

From the OP AlexKey's test, it requires that the working tree has been checked out (fully) at least once.