如何从Git终端编辑.git/config文件?

9
我正在尝试通过git终端修改配置文件。为此,在存储库内,我启动了命令git global --edit。然后,一个编辑器在同一终端中打开,我的问题是,如何保存更改并退出编辑器?每次我进行更改时,编辑器都会崩溃,我必须重新开始。是否有更容易的方法在终端之外完成这个任务?
2个回答

16

我已经启动了命令 git global --edit

不确定这个命令对你来说是如何有效的,也不清楚为什么它会生效。实际上,它本应该无法生效。 如果你想要在编辑器中全局修改配置,应该使用以下命令:

git config --global --edit

这应该会打开一个文本编辑器,让您进行更改,保存并退出编辑器。这应该有效。
如果您不想使用命令行但仍希望能够编辑git配置,则可以在您的主目录$HOME/.gitconfig中找到.gitconfig文件。
注意:您还可以通过在git bash中执行以下命令来更改git命令打开的默认编辑器:
git config --global core.editor "<editor-name> --wait"

<editor-name> 替换为你当前系统上安装的编辑器。对于VS Code,命令将是:
git config --global core.editor "code --wait"

好的,我尝试一下这个命令,那么如何保存更改并退出编辑器呢?我应该使用哪些命令? - sinfryd95
我相信它是在vi编辑器中打开的。对于Windows系统,我可以提供下列步骤:如果确实是这种情况,请修改文件并按ESC键,然后输入:wq再按回车(Windows上的“Enter”键)。 wq命令可保存您所做的更改并退出程序。只需输入:q即可退出而不保存更改。 - Asif Kamran Malick
我在我的回答中添加了一些有用的信息。希望这能让你免受未来的挫败感。 - Asif Kamran Malick
请注意,如果编辑器所在的目录中有空格,请用 ' 将其括起来,例如 "'C:\Program Files\Myeditor' -multiInst -nosession" - ΩmegaMan
您也可以将默认编辑器设置为Notepad++:git config --global core.editor "notepad++"(这里不需要--wait)。 - Matt

6
  1. Open Git Bash.

  2. Enter the command below:

    git config --global --edit 
    
  3. Press 'i' to enable edit -- i.e i

  4. Enter or modify the user:

    For example:

    [User1] i.e can use any name like Personal, Business, other, etc..
    name = <github username>
    email = <github email>
    [User2] i.e can use any name like Personal, Business, other, etc..
    name = <github username>
    email = <github email>
    
  5. Press - esc button.

  6. Now cursor will move at bottom. enter :wq and press the Enter button to exit. i.e :wq

  7. Verify the detail by using:

    git config --list
    

注意:除命令外,Windows系统可能有所不同


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接