bash root 到 git pull 作为另一个用户的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19872235/
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
Script for root to git pull as another user
提问by tubaguy50035
I have a script that I would like to have do a git pull inside another user's git directory. This script is run by the root user. For example:
我有一个脚本,我想在另一个用户的 git 目录中执行 git pull。该脚本由 root 用户运行。例如:
cd /home/username/GitProject
sudo -u username -i git pull
When I run this, I get:
当我运行这个时,我得到:
fatal: Not a git repository (or any of the parent directories): .git
Is there a way to have my script do a git pull as username
?
有没有办法让我的脚本做一个 git pull as username
?
回答by John Zwinck
Try without the -i
option to sudo. That option is documented as first changing to the target user's home directory, which undoes the directory change you so carefully do before that. Alternatively, use the appropriate options to git to specify the directory, something like this:
在没有-i
sudo 选项的情况下尝试。该选项被记录为首先更改到目标用户的主目录,这会撤消您在此之前小心执行的目录更改。Alternatively, use the appropriate options to git to specify the directory, something like this:
sudo -u username -i git --git-dir=/home/username/GitProject/.git --work-tree=/home/username/GitProject pull
回答by linux_sa
This can be done without sudo. This assumes you have password-less ssh keys since you are talking about a script. Here's the failure:
这可以在没有 sudo 的情况下完成。这假设您拥有无密码的 ssh 密钥,因为您在谈论脚本。这是失败的原因:
# git clone <user>@<host>:/path/to/repo
Cloning into 'repo'...
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
This shows that ~
properly expands to the user's homedir:
这表明~
正确扩展到用户的主目录:
# MYUSER=somebody
# su - $MYUSER -c "echo ~"
/home/somebody
And here's the actual command used to clone into the home directory along with some extra proofs:
这是用于克隆到主目录的实际命令以及一些额外的证明:
# su - $MYUSER -c "git clone <user>@<host>:/path/to/repo"
Cloning into 'repo'...
remote: Counting objects: 13, done.
<..>
# ls -l /home/$MYUSER/repo/.git/config
-rw-r--r-- 1 somebody somebody 275 Nov 8 23:55 /home/somebody/repo/.git/config
# su - $MYUSER -c "cd ~/repo; git remote -v"
origin <user>@<host>:/path/to/repo (fetch)
origin <user>@<host>:/path/to/repo (push)
# su - $MYUSER -c "cd ~/repo; git pull"
Already up-to-date.