简体   繁体   English

如何通过Shell脚本运行git命令?

[英]How can I run git commands via shell script?

I have my main repo on my server and two clones. 我在服务器上有主存储库,并且有两个克隆。

One clone is on my local for editing. 我的本地上有一个克隆供编辑。 Another copy is on my server in the public folder to pull the changes and view them live. 我服务器上公用文件夹中的另一个副本可拉出更改并实时查看。

The current routine I'm going through is edit local -> log into my server to the public folder and do a git pull to see the updated changes. 我正在经历的当前例程是编辑本地->登录到服务器到公用文件夹,然后执行git pull来查看更新的更改。

I would like to use the git hooks "post-receive" shell script to change into the public folder and do a git pull origin master...I just can't seem to figure out how to do this properly. 我想使用git hooks“ post-receive” shell脚本更改为公用文件夹并执行git pull origin master ...我似乎无法弄清楚如何正确执行此操作。 I tried this using: 我尝试使用以下方法:

cd /home/demo/public_html/example.com/public/ && `git status`

But the return I get is "not a repository" 但是我得到的回报是“不是存储库”

However, if I run cd /home/demo/public_html/example.com/public/ && ls 但是,如果我运行cd /home/demo/public_html/example.com/public/ && ls

I WILL get the contents of the public folder (so I'm moving into the right place) Thanks in advanced! 我将获取公用文件夹的内容(所以我要移到正确的位置)谢谢高级!

UPDATE: 更新:

Should have mentioned I was trying this with and without ticks 应该提到我正在尝试有无刻度

首先,在执行git pull或git status测试之前,尝试取消设置GIT_DIR env变量。

unset GIT_DIR

I think you want this: 我想你想要这个:

cd /home/demo/public_html/example.com/public/ && git status

The backticks are used to capture the output of a command in shell variable, for example: 反引号用于捕获shell变量中命令的输出,例如:

x=`ls`

will put the output of ls into the variable x . 会将ls的输出放入变量x

Are you actually including the backquotes around git status ? 您实际上是否在git status周围添加了反引号? That is, are you writing cd ... && `git status` or cd ... && git status ? 也就是说,您是在写cd ... && `git status`还是cd ... && git status吗? If you're writing the first one, then what that's doing is evaluating the git status command, and then trying to interpret the results of that as another command to execute. 如果您正在编写第一个命令,那么这样做是在评估git status命令,然后尝试将其结果解释为另一个要执行的命令。 Remove the backquotes, and you should just get the result of git status , not an error as it tries to interpret its results as a command. 删除反引号,您应该只得到git status的结果,而不是错误,因为它试图将其结果解释为命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM