Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git - checkout single file under bare repository

Tags:

git

sh

githooks

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?

like image 297
Alexey Kamenskiy Avatar asked Nov 28 '12 10:11

Alexey Kamenskiy


1 Answers

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.

like image 73
VonC Avatar answered Oct 06 '22 07:10

VonC