如何在 Windows 上更改 git bash 用户名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7816131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:17:31  来源:igfitidea点击:

How to change the git bash username on Windows?

windowsgitwindows-7

提问by MarkovCh1

Assume that my username in Windows 7 is Caesar. When I open Git Bash I am greeted with something like

假设我在 Windows 7 中的用户名是 C​​aesar。当我打开 Git Bash 时,我会看到类似的东西

Caesar@COMPUTER-NAME$

Caesar@COMPUTER-NAME$

Is it possible to change my username to be lowercase (so that it agrees with various Linux servers I have):

是否可以将我的用户名更改为小写(以便它与我拥有的各种 Linux 服务器一致):

caesar@COMPUTER-NAME$

caesar@COMPUTER-NAME$

P.S. In cygwin, one can edit /etc/passwdin an obvious way to achieve this, but there is no such file for git bash (might be useful).

PS 在 cygwin 中,可以/etc/passwd通过一种明显的方式进行编辑来实现这一点,但是 git bash 没有这样的文件(可能有用)。

回答by Dan Halbert

You can use the ssh_config facility to specify a different username. See an ssh_config manpage for details, but briefly:

您可以使用 ssh_config 工具指定不同的用户名。有关详细信息,请参阅 ssh_config 联机帮助页,但简要说明:

Create the file ~/.ssh/config, and put just this line in it:

创建文件~/.ssh/config,并在其中放入以下行:

User caesar

If you have different usernames for different hosts, you can use the Host setting to specify different usernames, including the default one:

如果不同的主机有不同的用户名,可以使用 Host 设置来指定不同的用户名,包括默认的:

Host rome1
  User caesar
Host rome2
  User brutus
Host *
  User romeo

Normally, the ~/.ssh/configfile has to have mode 600, but that doesn't seem to be necessary for the Git windows version.

通常,该~/.ssh/config文件必须具有模式 600,但对于 Git windows 版本似乎没有必要。

回答by manojlds

It is based on what is set to the $PS1, which is what is used to display your prompt. The username@computername part would have been generated by a value like \u@\h$. I am not aware of easy ways to make \uvalue lowercase, but in your profile, you can do some processing with the username and use the necessary lowercase value ( or anything) that you want and set it to the $PS1

它基于设置为 的内容$PS1,即用于显示提示的内容。username@computername 部分将由类似的值生成\u@\h$。我不知道将\u值设置为小写的简单方法,但在您的个人资料中,您可以对用户名进行一些处理,并使用您想要的必要小写值(或任何内容)并将其设置为$PS1

回答by Douglas Medeiros

This thread is a few years old, but the correct answer is to change the value of the variable that holds that information.

该线程已有几年历史,但正确的答案是更改保存该信息的变量的值。

If you modify the git-prompt.sh script a bit (for me, this is located in c:\Program Files(x86)\Git\etc\profile.d\git-prompt.sh), you can make the title anything you want.

如果您稍微修改 git-prompt.sh 脚本(对我来说,它位于c:\Program Files(x86)\Git\etc\profile.d\git-prompt.sh),您可以将标题设为任何内容你要。

Note: You will need to run VS Code, Notepad ++ or similar as administrator to write back to this directory.

注意:您需要以管理员身份运行 VS Code、Notepad ++ 或类似程序才能写回此目录。

PS1='\[3]0;$TITLEPREFIX:$PWD
...
PS1="$PS1"'STACKOVERFLOW@ABC '    # user@host<space>
...
PS1="$PS1"'LOL '                  # show MSYSTEM
...
7\]' # set window title PS1="$PS1"'\n' # new line PS1="$PS1"'\[3[32m\]' # change to green PS1="$PS1"'\u@\h ' # user@host<space> PS1="$PS1"'\[3[35m\]' # change to purple PS1="$PS1"'$MSYSTEM ' # show MSYSTEM PS1="$PS1"'\[3[33m\]' # change to brownish yellow PS1="$PS1"'\w' # current working directory

example:

例子:

STACKOVERFLOW@ABC LOL /
$

console:

安慰:

##代码##

回答by giulianvivan

It is not exactly what you want, but you can also clone/edit your remotes to have the URL with the username included. Ex.: git clone ssh://[email protected]

这并不完全是您想要的,但您也可以克隆/编辑遥控器以包含包含用户名的 URL。例如:git clone ssh://[email protected]

回答by Pradeep Kumar Prabaharan

To change windows usernamealso with Git bash username(not for upper case):

使用Git bash 用户名(不是大写)更改Windows 用户

  • Open Command prompt
  • Enter netplwiz
  • Select the windows user account and click the Properties button
  • Enter the new name for the account
  • Save and restart your computer
  • 打开命令提示符
  • 进入netplwiz
  • 选择 Windows 用户帐户,然后单击“属性”按钮
  • 输入帐户的新名称
  • 保存并重新启动计算机