【问题标题】:Script for root to git pull as another userroot 以 git pull 作为另一个用户的脚本
【发布时间】:2013-11-21 05:46:46
【问题描述】:

我有一个脚本,我想在另一个用户的 git 目录中执行 git pull。此脚本由 root 用户运行。例如:

cd /home/username/GitProject
sudo -u username -i git pull

当我运行它时,我得到:

fatal: Not a git repository (or any of the parent directories): .git

有没有办法让我的脚本以username 的身份执行 git pull?

【问题讨论】:

  • 尝试将git pull 替换为pwd 并查看打印的内容。
  • 我收到/home/username

标签: linux git bash ubuntu


【解决方案1】:

尝试不使用 sudo 的 -i 选项。该选项被记录为首先更改为目标用户的主目录,这会撤消您在此之前仔细进行的目录更改。或者,使用适当的 git 选项来指定目录,如下所示:

sudo -u username -i git --git-dir=/home/username/GitProject/.git --work-tree=/home/username/GitProject pull

【讨论】:

  • 我添加了额外的 git 参数。谢谢!
【解决方案2】:

这可以在没有 sudo 的情况下完成。这假设您有无密码的 ssh 密钥,因为您正在谈论脚本。这是失败的原因:

# git clone <user>@<host>:/path/to/repo
Cloning into 'repo'...
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

这表明~ 正确地扩展到用户的主目录:

# MYUSER=somebody
# su - $MYUSER -c "echo ~"
/home/somebody

这是用于克隆到主目录的实际命令以及一些额外的证明:

# 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.

【讨论】:

    猜你喜欢
    • 2013-11-13
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2012-03-11
    • 2012-03-22
    • 1970-01-01
    相关资源
    最近更新 更多