[英]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
兩者:
檢查配置是否確實已導入:
gsudo config --list --show-origin --includes | less
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.