You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Git中设置包含空格的配置值?(Windows 11 Git Bash)

解决Git Bash中设置含空格用户名失败的问题

问题根源

你自定义的git函数里,command git $@这一行没有给$@添加双引号。在Bash中,未加引号的$@会将带空格的参数拆分成多个独立参数,导致含空格的用户名被截断,或者-c参数里的user.name=John Doe被拆成user.name=John和Doe,后者被误识别为Git命令。

临时解决方法(立即生效)

绕过自定义的git别名,直接调用原生Git命令设置用户名:

command git config --global user.name "Your Name"

验证是否成功:

command git config user.name

永久修复(修改自定义git函数)

找到定义该git函数的配置文件(通常是~/.bashrc或~/.bash_profile),将函数中的command git $@修改为command git "$@",同时建议给${@:2}也加上双引号,避免克隆带空格的仓库路径时出问题。修改后的完整函数如下:

go_to_folder() {
  cd "$1" || return 1
}

git ()
{
    if [ "$1" = "clonecd" ]; then
        output=$(command git clone --progress "${@:2}" 2>&1 | tee /dev/stderr);
        dir=$(echo "$output" | sed -n "s/Cloning into '\([^']*\)'\.\.\..*/\1/p;q");
        go_to_folder "$dir";
    else
        command git "$@";
    fi
}

修改后执行以下命令让配置生效:

source ~/.bashrc

之后就可以正常使用常规命令设置含空格的用户名:

git config --global user.name "Your Name"

也能正常使用带空格参数的-c命令:

git -c user.name='John Doe' gui

内容的提问来源于stack exchange,提问作者elechris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 01:33:13