bash git post-update 脚本不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9228921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git post-update script does not work
提问by PiTheNumber
After editing my old questiona few times, I make a new one because it is a new question now.
在我的旧问题编辑了几次之后,我提出了一个新问题,因为它现在是一个新问题。
In .git/hooks/post-updateI have:
在.git/hooks/post-update我有:
echo "a" >> /home/pi/log
git update-server-info
git stash
git merge testing >> /home/pi/log
To make an automated checkout. So I run on the client:
进行自动结账。所以我在客户端上运行:
git push testing HEAD:testing
Now my /home/pi/logcontains:
现在我的/home/pi/log包含:
a
Updating ae2f44b..04753a9
Fast-forward
application/views/main/index.php | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
But the file did not change!
但是文件没有改变!
$ git merge testing
Already up-to-date.
If I remove the script, make the push and run git stash, git merge testingit works.
如果我删除脚本,进行 push 和 run git stash,git merge testing它就可以工作。
Update
更新
For testing I changed a number in a file from 17 to 20. I can see the right file version if I run
为了测试,我将文件中的数字从 17 更改为 20。如果我运行,我可以看到正确的文件版本
git show application/views/main/index.php
but
但
vim application/views/main/index.php
Still contains the old number. But git claims the file is updated:
仍然包含旧号码。但是 git 声称文件已更新:
$ git merge testing
Already up-to-date.
采纳答案by PiTheNumber
The solution is to use post-receiveas Alex pointed out. Also you need run unset GIT_DIRat the top of your script.
解决方案是post-receive像亚历克斯指出的那样使用。您还需要unset GIT_DIR在脚本的顶部运行。
On the server I have created a second branch and switched to it:
在服务器上,我创建了第二个分支并切换到它:
$ git branch
master
* testing
My .git/hooks/post-receivenow looks like this:
我.git/hooks/post-receive现在看起来像这样:
unset GIT_DIR
cd ..
git merge master
On the client I run git push.
在客户端上,我运行git push.
回答by sehe
EDIT
编辑
It looks like this is your problem:
看起来这是你的问题:
pre-receive update post-receive post-updateThese hooks can be run either in a bare or a non-bare repository. In both cases, the current working directory will be the git directory. So, if this is a bare repository called "/src/git/test.git/", that will be the current working directory -- if this is a non-bare repository and the top level of the working tree is "/home/mark/test/" then the current working directory will be "/home/mark/test/.git/".
In both cases, the following environment variable is set:
GIT_DIRis set to ‘.'With a working tree, this is unexpectedly awkward, as described in Chris Johnsen's answer that I linked to earlier. If only
GIT_DIRis set then this comment from the git man page applies:Note: If
--git-dirorGIT_DIRare specified but none of--work-tree,GIT_WORK_TREEandcore.worktreeis specified, the current working directory is regarded as the top directory of your working tree.In other words, your working tree will also be the current directory (the ".git" directory), which almost certainly isn't what you want.
pre-receive update post-receive post-update这些钩子可以在裸仓库或非裸仓库中运行。在这两种情况下,当前工作目录都是 git 目录。所以,如果这是一个名为“/src/git/test.git/”的裸仓库,那将是当前的工作目录——如果这是一个非裸仓库并且工作树的顶层是“/home” /mark/test/" 那么当前的工作目录将是 "/home/mark/test/.git/"。
在这两种情况下,都设置了以下环境变量:
GIT_DIR设置为 '.'使用工作树,这出乎意料地尴尬,正如我之前链接的 Chris Johnsen 的回答中所述。如果
GIT_DIR设置了only ,则 git 手册页中的此注释适用:注意:如果指定了
--git-dir或GIT_DIR,但没有指定--work-tree,GIT_WORK_TREE和core.worktree,则当前工作目录被视为工作树的顶层目录。换句话说,您的工作树也将是当前目录(“.git”目录),这几乎肯定不是您想要的。
You could try setting GIT_WORK_TREE=..or GIT_WORK_TREE="$GIT_DIR/.."inside the hook
您可以尝试设置GIT_WORK_TREE=..或GIT_WORK_TREE="$GIT_DIR/.."在钩子内
But the file did not change!
但是文件没有改变!
Most likely it did. Perhaps only the lineending did, or there were whitespace changes that are ignored when viewing the diffs, but it did change. Git knows, because the SHA1 sum of the file content changed.
很可能是这样。也许只有行尾做了,或者在查看差异时忽略了空白更改,但它确实发生了变化。Git 知道,因为文件内容的 SHA1 总和发生了变化。
Are you using windows on either end?
你在两端都使用窗户吗?
Windows has a tendency to mess with the line-endings. See the core.autocrlf and related options:
Windows 倾向于弄乱行尾。查看 core.autocrlf 和相关选项:
回答by Alex
Did you try to use the post-receivehook instead? Maybe something is not yet finished in the post-updateand that is why the merge is not working.
您是否尝试使用post-receive钩子代替?也许有些东西还没有完成post-update,这就是合并不起作用的原因。
Also I think you should try to include git reset --hardin the script so that the git status is synchronised with the file system.
另外我认为您应该尝试包含git reset --hard在脚本中,以便 git status 与文件系统同步。

