简体   繁体   English

使用sudo使git commit

[英]Make git commit using sudo

How can one make a git commit of a folder where they don't have write access? 如何在没有写访问权的文件夹中进行git commit? My initial attempt was: 我最初的尝试是:

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

Unfortunately, this will not work. 不幸的是,这是行不通的。 The regular user has a .gitconfig with the required name and email and I need to have git use those credentials in the commit. 普通用户有一个.gitconfig带有所需的名称和电子邮件,我需要让git在提交中使用这些凭据。 The root user does not have such git credentials, and therefore cannot make a commit. root用户没有这种git凭据,因此无法提交。

git has -c option that let's you override values set in .gitconfig (or provide any new key-value pairs): 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).

So you could for example execute: 因此,您可以例如执行:

git -c user.name=user1032531 init

You could also link your user's .gitconfig as root's gitconfig or make the grant the current user access to all the files that need to be in the repo (consider using the last option). 您还可以将用户的.gitconfig链接为root用户的.gitconfig ,或使授予当前用户访问仓库中所有需要的文件的权限(考虑使用最后一个选项)。

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

To automatically pull in my non-root user's config files, I use the bash alias: 为了自动提取非root用户的配置文件,我使用bash别名:

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

Then I use gsudo instead of git to both: 然后我使用gsudo而不是git两者:

  • Run as root 以root身份运行
  • Have access to all non-root user git configuration 有权访问所有非root用户的git配置

Check that the config is indeed being imported: 检查配置是否确实已导入:

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

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

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