繁体   English   中英

使用sudo使git commit

[英]Make git commit using sudo

如何在没有写访问权的文件夹中进行git commit? 我最初的尝试是:

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

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

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 ,或使授予当前用户访问仓库中所有需要的文件的权限(考虑使用最后一个选项)。

您在此处有3个选项,您可以自己授予该文件夹的权限(即,使其可以被任何人使用),可以更改要提交到具有烫发的位置的目录,或者可以编辑git的配置文件sudo使用sudo编辑文件。

为了自动提取非root用户的配置文件,我使用bash别名:

alias gsudo='sudo git -c "include.path='"${XDG_CONFIG_DIR:-$HOME/.config}/git/config\" -c \"include.path=$HOME/.gitconfig\""

然后我使用gsudo而不是git两者:

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

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

gsudo config --list --show-origin --includes | less

暂无
暂无

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

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