使用sudo进行git提交

时间:2017-05-26 12:37:50

标签: linux git

如何对没有写入权限的文件夹进行git提交?我最初的尝试是:

normaluser@host:/etc $ sudo git init
normaluser@host:/etc $ sudo git commit -m'init'

不幸的是,这不起作用。普通用户有一个.gitconfig,其中包含所需的名称和电子邮件,我需要让git在提交中使用这些凭据。 root用户没有这样的git凭据,因此无法进行提交。

3 个答案:

答案 0 :(得分:3)

git有-c选项,让您覆盖.gitconfig中设置的值(或提供任何新的键值对):

   -c <name>=<value>
       Pass a configuration parameter to the command. The value given will override
       values from configuration files. The <name> is expected in the same format as
       listed by git config (subkeys separated by dots).

所以你可以执行:

git -c user.name=user1032531 init

您还可以将用户的.gitconfig链接为root的gitconfig,或者授予当前用户访问需要在repo中的所有文件的权限(请考虑使用最后一个选项)。

答案 1 :(得分:2)

这里有3个选项,要么你自己给该文件夹的权限(即任何人都可以使用),你可以将你想要提交的内容的目录更改为你有权限的地方,或者你可以编辑git的sudo配置文件使用sudo编辑文件。

答案 2 :(得分:0)

要自动提取非root用户的配置文件,请使用x别名:

bash

然后我同时使用alias gsudo='sudo git -c "include.path='"${XDG_CONFIG_DIR:-$HOME/.config}/git/config\" -c \"include.path=$HOME/.gitconfig\"" 而不是gsudo

  • 以root身份运行
  • 有权访问所有非root用户的git配置

检查配置是否确实已导入:

git