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

发布于 2024-12-10 19:34:30 字数 318 浏览 1 评论 0原文

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

凯撒@COMPUTER-NAME$

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

凯撒@COMPUTER-NAME$

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

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

Caesar@COMPUTER-NAME$

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

caesar@COMPUTER-NAME$

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

半葬歌 2024-12-17 19:34:30

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

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

User caesar

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

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

通常,~/.ssh/config 文件必须具有模式 600,但这对于 Git windows 版本似乎不是必需的。

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

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

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 rome1
  User caesar
Host rome2
  User brutus
Host *
  User romeo

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

橘香 2024-12-17 19:34:30

要同时使用 Git bash 用户名更改 windows 用户名(不能为大写)

  • 打开命令提示符
  • 输入 netplwiz
  • 选择 Windows 用户帐户,然后单击属性按钮
  • 输入帐户的新名称
  • 保存并重新启动计算机

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

  • 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
我乃一代侩神 2024-12-17 19:34:30

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

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

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

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

示例:

...
PS1="$PS1"'STACKOVERFLOW@ABC '    # user@host<space>
...
PS1="$PS1"'LOL '                  # show MSYSTEM
...

控制台:

STACKOVERFLOW@ABC LOL /
$

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.

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

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

example:

...
PS1="$PS1"'STACKOVERFLOW@ABC '    # user@host<space>
...
PS1="$PS1"'LOL '                  # show MSYSTEM
...

console:

STACKOVERFLOW@ABC LOL /
$
残花月 2024-12-17 19:34:30

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

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 \u value 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

明月松间行 2024-12-17 19:34:30

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

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]

薄荷→糖丶微凉 2024-12-17 19:34:30

在 Windows 上,您可以通过在 C:\Users\{userName}\.config\git\ 创建一个文件来实现此目的,并在此文件夹中创建一个名为 git-prompt 的文件.sh

在此文件中,您可以编写以下代码

USER="Dev"                     # You can use any name you want to see in terminal

PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
PS1="$PS1"'\n'                 # new line
PS1="$PS1"'\[\033[32m\]'       # change to green
PS1="$PS1"'$USER@\h '          # user@host<space>
PS1="$PS1"'\[\033[35m\]'       # change to purple
PS1="$PS1"'$MSYSTEM '          # show MSYSTEM
PS1="$PS1"'\[\033[33m\]'       # change to brownish yellow
PS1="$PS1"'\w'                 # current working directory
if test -z "$WINELOADERNOEXEC"
then
    GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
    COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
    COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
    COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
    if test -f "$COMPLETION_PATH/git-prompt.sh"
    then
        . "$COMPLETION_PATH/git-completion.bash"
        . "$COMPLETION_PATH/git-prompt.sh"
        PS1="$PS1"'\[\033[36m\]'  # change color to cyan
        PS1="$PS1"'`__git_ps1`'   # bash function
    fi
fi
PS1="$PS1"'\[\033[0m\]'        # change color
PS1="$PS1"'\n'                 # new line
PS1="$PS1"'$ '                 # prompt: always $

该文件基本上由 git 使用。如果此文件存在,git 将查找此文件,否则它将使用存在于以下位置的默认配置
C:\Program Files(x86)\Git\etc\profile.d\git-prompt.sh - 适用于 x86/32 位系统

c:\Program Files\Git\etc\profile.d\git-prompt.sh 适用于标准 x64 位系统。

上面的代码片段也从同一个文件中使用。

On windows, you can achieve this by creating a file at C:\Users\{userName}\.config\git\ and inside this folder you can create a file with name git-prompt.sh.

Inside this file you can write following code

USER="Dev"                     # You can use any name you want to see in terminal

PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
PS1="$PS1"'\n'                 # new line
PS1="$PS1"'\[\033[32m\]'       # change to green
PS1="$PS1"'$USER@\h '          # user@host<space>
PS1="$PS1"'\[\033[35m\]'       # change to purple
PS1="$PS1"'$MSYSTEM '          # show MSYSTEM
PS1="$PS1"'\[\033[33m\]'       # change to brownish yellow
PS1="$PS1"'\w'                 # current working directory
if test -z "$WINELOADERNOEXEC"
then
    GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
    COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
    COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
    COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
    if test -f "$COMPLETION_PATH/git-prompt.sh"
    then
        . "$COMPLETION_PATH/git-completion.bash"
        . "$COMPLETION_PATH/git-prompt.sh"
        PS1="$PS1"'\[\033[36m\]'  # change color to cyan
        PS1="$PS1"'`__git_ps1`'   # bash function
    fi
fi
PS1="$PS1"'\[\033[0m\]'        # change color
PS1="$PS1"'\n'                 # new line
PS1="$PS1"'$ '                 # prompt: always $

This file is basically used by git. If this file exits, git will look for this file else it will use default config present at
C:\Program Files(x86)\Git\etc\profile.d\git-prompt.sh - for x86/32 bit system
and
c:\Program Files\Git\etc\profile.d\git-prompt.sh for a standard x64 bit system.

This above code snippet is also used from same file.

地狱即天堂 2024-12-17 19:34:30

步骤 1 只需转到您计算机的此位置 => C:\Program Files\Git\etc\profile.d

步骤2 在那里你会发现一个文件“git-prompt.sh”,用记事本以外的任何工具打开它。

Step-3 然后按照这个步骤=> https://ibb.co/xGxQ5Cf

工作已完成。

或者,您可以关注此 YouTube 视频 => https://youtu.be/VIWKc5sx5Ao?si=10UiAbDKqM-O5wQd

Step-1 Just go to this location of your computer => C:\Program Files\Git\etc\profile.d

Step-2 There you will find a file "git-prompt.sh", open it with anything other than notepad.

Step-3 Then follow this step => https://ibb.co/xGxQ5Cf

The work is finished.

Or, you can follow along this youtube video => https://youtu.be/VIWKc5sx5Ao?si=10UiAbDKqM-O5wQd

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文