在 Windows 安装的 Git Bash 中更改 ~ 目录的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32232978/
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-09 03:31:08  来源:igfitidea点击:

Change the location of the ~ directory in a Windows install of Git Bash

gitmsysgitgit-bashmingw32msys2

提问by PEWColina

I am not even sure I am asking the right question. Let me explain my situation:

我什至不确定我问的是对的问题。说说我的情况:

This is about Git on Windows 7.

这是关于 Windows 7 上的 Git。

My company sets up the Windows user directory on a network drive, not on the local hard drive (for backup and other purposes beyond the scope of this question). I cannot change that policy.

我的公司在网络驱动器上设置 Windows 用户目录,而不是在本地硬盘驱动器上(用于备份和其他超出本问题范围的目的)。我不能改变那个政策。

I CAN have local files outside of that scheme however and that is how my Apache server is set up. Entirely local.

但是,我可以在该方案之外拥有本地文件,这就是我的 Apache 服务器的设置方式。完全是本地的。

I installed Git. It installs Bash. When I fire up Bash and cd ~(change to the root directory) I find that it (the Bash root) points to my network user directory. I want it not to do that. I want ~to be elsewhere on my hard drive. I want it so that when I perform other operations such as installing a certificate, an SSH key, etc. its defaults will not automatically use that network directory.

我安装了 Git。它安装 Bash。当我启动 Bash 并且cd ~(切换到根目录)时,我发现它(Bash 根)指向我的网络用户目录。我希望它不要那样做。我想 ~在我的硬盘上的其他地方。我想要它,以便在我执行其他操作(例如安装证书、SSH 密钥等)时,其默认值不会自动使用该网络目录。

I have searched in vain everywhere, and all I can find refers to answers that involve aliases, redirection, and the location $HOME points to. But that is not what I want at all.

我到处搜索都徒劳无功,我能找到的都是涉及别名、重定向和$HOME指向的位置的答案。但这根本不是我想要的。

The question is: Can I change that directory? And if so: How?

问题是:我可以更改该目录吗?如果是这样:如何?

UPDATE: So, $HOME is what I need to modify. However I have been unable to find where this mythical $HOME variable is set so I assumed it was a Linux system version of PATH or something. Anyway...

更新:所以, $HOME 是我需要修改的。但是我一直无法找到这个神秘的 $HOME 变量的设置位置,所以我认为它是 PATH 的 Linux 系统版本或其他东西。反正...

I do have a "profile" file under git/etc. Here are the contents (notice no $HOME):

我在git/etc. 以下是内容(注意没有 $HOME):

  # To the extent possible under law, the author(s) have dedicated all
  # copyright and related and neighboring rights to this software to the
  # public domain worldwide. This software is distributed without any warranty.
  # You should have received a copy of the CC0 Public Domain Dedication along
  # with this software.
  # If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.


  # System-wide profile file

  # Some resources...
  # Customizing Your Shell: http://www.dsl.org/cookbook/cookbook_5.html#SEC69
  # Consistent BackSpace and Delete Configuration:
  #   http://www.ibb.net/~anne/keyboard.html
  # The Linux Documentation Project: http://www.tldp.org/
  # The Linux Cookbook: http://www.tldp.org/LDP/linuxcookbook/html/
  # Greg's Wiki http://mywiki.wooledge.org/

  # Setup some default paths. Note that this order will allow user installed
  # software to override 'system' software.
  # Modifying these default path settings can be done in different ways.
  # To learn more about startup files, refer to your shell's man page.

  MSYS2_PATH="/usr/local/bin:/usr/bin:/bin"
  MANPATH="/usr/local/man:/usr/share/man:/usr/man:/share/man:${MANPATH}"
  INFOPATH="/usr/local/info:/usr/share/info:/usr/info:/share/info:${INFOPATH}"
  MINGW_MOUNT_POINT=
  if [ -n "$MSYSTEM" ]
  then
    case "$MSYSTEM" in
      MINGW32)
        MINGW_MOUNT_POINT=/mingw32
        PATH="${MINGW_MOUNT_POINT}/bin:${MSYS2_PATH}:${PATH}"
        PKG_CONFIG_PATH="${MINGW_MOUNT_POINT}/lib/pkgconfig:${MINGW_MOUNT_POINT}/share/pkgconfig"
        ACLOCAL_PATH="${MINGW_MOUNT_POINT}/share/aclocal:/usr/share/aclocal"
        MANPATH="${MINGW_MOUNT_POINT}/share/man:${MANPATH}"
      ;;
      MINGW64)
        MINGW_MOUNT_POINT=/mingw64
        PATH="${MINGW_MOUNT_POINT}/bin:${MSYS2_PATH}:${PATH}"
        PKG_CONFIG_PATH="${MINGW_MOUNT_POINT}/lib/pkgconfig:${MINGW_MOUNT_POINT}/share/pkgconfig"
        ACLOCAL_PATH="${MINGW_MOUNT_POINT}/share/aclocal:/usr/share/aclocal"
        MANPATH="${MINGW_MOUNT_POINT}/share/man:${MANPATH}"
      ;;
      MSYS)
        PATH="${MSYS2_PATH}:/opt/bin:${PATH}"
        PKG_CONFIG_PATH="/usr/lib/pkgconfig:/usr/share/pkgconfig:/lib/pkgconfig"
      ;;
      *)
        PATH="${MSYS2_PATH}:${PATH}"
      ;;
    esac
  else
    PATH="${MSYS2_PATH}:${PATH}"
  fi

  MAYBE_FIRST_START=false
  SYSCONFDIR="${SYSCONFDIR:=/etc}"

  # TMP and TEMP as defined in the Windows environment must be kept
  # for windows apps, even if started from msys2. However, leaving
  # them set to the default Windows temporary directory or unset
  # can have unexpected consequences for msys2 apps, so we define
  # our own to match GNU/Linux behaviour.
  ORIGINAL_TMP=$TMP
  ORIGINAL_TEMP=$TEMP
  #unset TMP TEMP
  #tmp=$(cygpath -w "$ORIGINAL_TMP" 2> /dev/null)
  #temp=$(cygpath -w "$ORIGINAL_TEMP" 2> /dev/null)
  #TMP="/tmp"
  #TEMP="/tmp"
  case "$TMP" in *\*) TMP="$(cygpath -m "$TMP")";; esac
  case "$TEMP" in *\*) TEMP="$(cygpath -m "$TEMP")";; esac
  test -d "$TMPDIR" || test ! -d "$TMP" || {
    TMPDIR="$TMP"
    export TMPDIR
  }


  # Define default printer
  p='/proc/registry/HKEY_CURRENT_USER/Software/Microsoft/Windows NT/CurrentVersion/Windows/Device'
  if [ -e "${p}" ] ; then
    read -r PRINTER < "${p}"
    PRINTER=${PRINTER%%,*}
  fi
  unset p

  print_flags ()
  {
    ((  & 0x0002 )) && echo -n "binary" || echo -n "text"
    ((  & 0x0010 )) && echo -n ",exec"
    ((  & 0x0040 )) && echo -n ",cygexec"
    ((  & 0x0100 )) && echo -n ",notexec"
  }

  # Shell dependent settings
  profile_d ()
  {
    local file=
    for file in $(export LC_COLLATE=C; echo /etc/profile.d/*.); do
      [ -e "${file}" ] && . "${file}"
    done

    if [ -n ${MINGW_MOUNT_POINT} ]; then
      for file in $(export LC_COLLATE=C; echo ${MINGW_MOUNT_POINT}/etc/profile.d/*.); do
        [ -e "${file}" ] && . "${file}"
      done
    fi
  }

  for postinst in $(export LC_COLLATE=C; echo /etc/post-install/*.post); do
    [ -e "${postinst}" ] && . "${postinst}"
  done

  if [ ! "x${BASH_VERSION}" = "x" ]; then
    HOSTNAME="$(/usr/bin/hostname)"
    profile_d sh
    [ -f "/etc/bash.bashrc" ] && . "/etc/bash.bashrc"
  elif [ ! "x${KSH_VERSION}" = "x" ]; then
    typeset -l HOSTNAME="$(/usr/bin/hostname)"
    profile_d sh
    PS1=$(print '3]0;${PWD}\n3[32m${USER}@${HOSTNAME} 3[33m${PWD/${HOME}/~}3[0m\n$ ')
  elif [ ! "x${ZSH_VERSION}" = "x" ]; then
    HOSTNAME="$(/usr/bin/hostname)"
    profile_d zsh
    PS1='(%n@%m)[%h] %~ %% '
  elif [ ! "x${POSH_VERSION}" = "x" ]; then
    HOSTNAME="$(/usr/bin/hostname)"
    PS1="$ "
  else
    HOSTNAME="$(/usr/bin/hostname)"
    profile_d sh
    PS1="$ "
  fi

  if [ -n "$ACLOCAL_PATH" ]
  then
    export ACLOCAL_PATH
  fi

  export PATH MANPATH INFOPATH PKG_CONFIG_PATH USER TMP TEMP PRINTER HOSTNAME PS1 SHELL tmp temp
  test -n "$TERM" || export TERM=xterm-256color

  if [ "$MAYBE_FIRST_START" = "true" ]; then
    sh /usr/bin/regen-info.sh

    if [ -f "/usr/bin/update-ca-trust" ]
    then
      sh /usr/bin/update-ca-trust
    fi

    clear
    echo
    echo
    echo "###################################################################"
    echo "#                                                                 #"
    echo "#                                                                 #"
    echo "#                   C   A   U   T   I   O   N                     #"
    echo "#                                                                 #"
    echo "#                  This is first start of MSYS2.                  #"
    echo "#       You MUST restart shell to apply necessary actions.        #"
    echo "#                                                                 #"
    echo "#                                                                 #"
    echo "###################################################################"
    echo
    echo
  fi
  unset MAYBE_FIRST_START

回答by David Ferenczy Rogo?an

I don't understand, why you don't want to set the $HOMEenvironment variable since that solves exactly what you're asking for.

我不明白,为什么您不想设置$HOME环境变量,因为这正好解决了您的要求。

cd ~doesn't mean change to the root directory, but change to the user's home directory, which is set by the $HOMEenvironment variable.

cd ~不是说改到根目录,而是改到用户家目录,由$HOME环境变量设置。

Quick'n'dirty solution

快速解决方案

Edit C:\Program Files (x86)\Git\etc\profileand set $HOMEvariable to whatever you want (add it if it's not there). A good place could be for example right after a condition commented by # Set up USER's home directory. It must be in the MinGW format, for example:

编辑变量C:\Program Files (x86)\Git\etc\profile并将其设置$HOME为您想要的任何值(如果不存在则添加它)。例如,一个好地方可以是在评论的条件之后# Set up USER's home directory。它必须是 MinGW 格式,例如:

HOME=/c/my/custom/home

Save it, open Git Bash and execute cd ~. You should be in a directory /c/my/custom/homenow.

保存它,打开 Git Bash 并执行cd ~. 你/c/my/custom/home现在应该在一个目录中。

Everything that accesses the user's profile should go into this directory instead of your Windows' profile on a network drive.

访问用户配置文件的所有内容都应进入此目录,而不是网络驱动器上的 Windows 配置文件。

Note:C:\Program Files (x86)\Git\etc\profileis shared by all users, so if the machine is used by multiple users, it's a good idea to set the $HOMEdynamically:

注意:C:\Program Files (x86)\Git\etc\profile是所有用户共享的,所以如果机器被多个用户使用,最好$HOME动态设置:

HOME=/c/Users/$USERNAME

Cleaner solution

更清洁的解决方案

Set the environment variable HOMEin Windows to whatever directory you want. In this case, you have to set it in Windows path format (with backslashes, e.g. c:\my\custom\home), Git Bash will load it and convert it to its format.

HOMEWindows 中的环境变量设置为您想要的任何目录。在这种情况下,您必须将其设置为 Windows 路径格式(带反斜杠,例如c:\my\custom\home),Git Bash 将加载它并将其转换为它的格式。

If you want to change the home directory for all users on your machine, set it as a system environment variable, where you can use for example %USERNAME%variable so every user will have his own home directory, for example:

如果要更改计算机上所有用户的主目录,请将其设置为系统环境变量,您可以在其中使用 example%USERNAME%变量,以便每个用户都有自己的主目录,例如:

HOME=c:\custom\home\%USERNAME%

If you want to change the home directory just for yourself, set it as a user environment variable, so other users won't be affected. In this case, you can simply hard-code the whole path:

如果您只想为自己更改主目录,请将其设置为用户环境变量,以免影响其他用户。在这种情况下,您可以简单地对整个路径进行硬编码:

HOME=c:\my\custom\home

回答by EliandroRibeiro

In my case, all I had to do was add the following User variableon Windows:

就我而言,我所要做的就是在 Windows 上添加以下用户变量

Variable name: HOME
Variable value: %USERPROFILE%

变量名称:HOME
变量值:%USERPROFILE%

How to set a Environment Variable(You can use the User variables for usernamesection if you are not a system administrator)

如何设置环境变量(如果您不是系统管理员,则可以使用用户名部分的用户变量

回答by Menachem

I'd share what I did, which works not only for Git, but MSYS/MinGW as well.

我会分享我所做的,这不仅适用于 Git,也适用于 MSYS/MinGW。

The HOMEenvironment variable is not normally set for Windows applications, so creating it through Windows did not affect anything else. From the Computer Properties (right-click on Computer - or whatever it is named - in Explorer, and select Properties, or Control Panel -> System and Security -> System), choose Advanced system settings, then Environment Variables...and create a new one, HOME, and assign it wherever you like.

HOME环境变量通常不设置Windows应用程序,所以通过Windows创建它没有影响到任何东西。从“计算机属性”(在资源管理器中右键单击“计算机”或任何名称,然后选择“属性”或“控制面板”->“系统和安全”->“系统”),选择Advanced system settings,然后Environment Variables...创建一个新的HOME,然后分配它随心所欲。

If you can't create new environment variables, the other answer will still work. (I went through the details of how to create environment variables precisely because it's so dificult to find.)

如果您无法创建新的环境变量,则其他答案仍然有效。(我详细介绍了如何创建环境变量,因为它很难找到。)

回答by dekkard

Instead of modifying the global profileyou could create the .bash_profilein your default $HOMEdirectory (e.g. C:\Users\WhateverUser\.bash_profile) with the following contents:

profile您可以.bash_profile在默认$HOME目录(例如C:\Users\WhateverUser\.bash_profile)中创建具有以下内容的全局变量,而不是修改全局变量:

export HOME="C:\my\projects\dir"
cd "$HOME" # if you'd like it to be the starting dir of the git shell

回答by ?zgür ?ztürk

1.Right click to Gitbash shortcut choose Properties
2.Choose "Shortcut" tab
3.Type your starting directory to "Start in" field
4.Remove "--cd-to-home" part from "Target" field

1.右键单击Gitbash快捷方式选择属性
2.选择“快捷方式”选项卡
3.在“开始”字段中
键入您的起始目录4.从“目标”字段中删除“--cd-to-home”部分

回答by Duncan Jones

So, $HOME is what I need to modify. However I have been unable to find where this mythical $HOME variable is set so I assumed it was a Linux system version of PATH or something. Anyway...**

所以, $HOME 是我需要修改的。但是我一直无法找到这个神秘的 $HOME 变量的设置位置,所以我认为它是 PATH 的 Linux 系统版本或其他东西。无论如何...**

Answer

回答

Adding HOME at the top of the profilefile worked.

profile文件顶部添加 HOME有效。

HOME="c://path/to/custom/root/".

HOME="c://path/to/custom/root/".

  #THE FIX WAS ADDING THE FOLLOWING LINE TO THE TOP OF THE PROFILE FILE

  HOME="c://path/to/custom/root/"

  # below are the original contents ===========
  # To the extent possible under law, ..blah blah

  # Some resources...
  # Customizing Your Shell: http://www.dsl.org/cookbook/cookbook_5.html#SEC69
  # Consistent BackSpace and Delete Configuration:
  #   http://www.ibb.net/~anne/keyboard.html
  # The Linux Documentation Project: http://www.tldp.org/
  # The Linux Cookbook: http://www.tldp.org/LDP/linuxcookbook/html/
  # Greg's Wiki http://mywiki.wooledge.org/

  # Setup some default paths. Note that this order will allow user installed
  # software to override 'system' software.
  # Modifying these default path settings can be done in different ways.
  # To learn more about startup files, refer to your shell's man page.

  MSYS2_PATH="/usr/local/bin:/usr/bin:/bin"
  MANPATH="/usr/local/man:/usr/share/man:/usr/man:/share/man:${MANPATH}"
  INFOPATH="/usr/local/info:/usr/share/info:/usr/info:/share/info:${INFOPATH}"
  MINGW_MOUNT_POINT=
  if [ -n "$MSYSTEM" ]
  then
    case "$MSYSTEM" in
      MINGW32)
        MINGW_MOUNT_POINT=/mingw32
        PATH="${MINGW_MOUNT_POINT}/bin:${MSYS2_PATH}:${PATH}"
        PKG_CONFIG_PATH="${MINGW_MOUNT_POINT}/lib/pkgconfig:${MINGW_MOUNT_POINT}/share/pkgconfig"
        ACLOCAL_PATH="${MINGW_MOUNT_POINT}/share/aclocal:/usr/share/aclocal"
        MANPATH="${MINGW_MOUNT_POINT}/share/man:${MANPATH}"
      ;;
      MINGW64)
        MINGW_MOUNT_POINT=/mingw64
        PATH="${MINGW_MOUNT_POINT}/bin:${MSYS2_PATH}:${PATH}"
        PKG_CONFIG_PATH="${MINGW_MOUNT_POINT}/lib/pkgconfig:${MINGW_MOUNT_POINT}/share/pkgconfig"
        ACLOCAL_PATH="${MINGW_MOUNT_POINT}/share/aclocal:/usr/share/aclocal"
        MANPATH="${MINGW_MOUNT_POINT}/share/man:${MANPATH}"
      ;;
      MSYS)
        PATH="${MSYS2_PATH}:/opt/bin:${PATH}"
        PKG_CONFIG_PATH="/usr/lib/pkgconfig:/usr/share/pkgconfig:/lib/pkgconfig"
      ;;
      *)
        PATH="${MSYS2_PATH}:${PATH}"
      ;;
    esac
  else
    PATH="${MSYS2_PATH}:${PATH}"
  fi

  MAYBE_FIRST_START=false
  SYSCONFDIR="${SYSCONFDIR:=/etc}"

  # TMP and TEMP as defined in the Windows environment must be kept
  # for windows apps, even if started from msys2. However, leaving
  # them set to the default Windows temporary directory or unset
  # can have unexpected consequences for msys2 apps, so we define
  # our own to match GNU/Linux behaviour.
  ORIGINAL_TMP=$TMP
  ORIGINAL_TEMP=$TEMP
  #unset TMP TEMP
  #tmp=$(cygpath -w "$ORIGINAL_TMP" 2> /dev/null)
  #temp=$(cygpath -w "$ORIGINAL_TEMP" 2> /dev/null)
  #TMP="/tmp"
  #TEMP="/tmp"
  case "$TMP" in *\*) TMP="$(cygpath -m "$TMP")";; esac
  case "$TEMP" in *\*) TEMP="$(cygpath -m "$TEMP")";; esac
  test -d "$TMPDIR" || test ! -d "$TMP" || {
    TMPDIR="$TMP"
    export TMPDIR
  }


  # Define default printer
  p='/proc/registry/HKEY_CURRENT_USER/Software/Microsoft/Windows NT/CurrentVersion/Windows/Device'
  if [ -e "${p}" ] ; then
    read -r PRINTER < "${p}"
    PRINTER=${PRINTER%%,*}
  fi
  unset p

  print_flags ()
  {
    ((  & 0x0002 )) && echo -n "binary" || echo -n "text"
    ((  & 0x0010 )) && echo -n ",exec"
    ((  & 0x0040 )) && echo -n ",cygexec"
    ((  & 0x0100 )) && echo -n ",notexec"
  }

  # Shell dependent settings
  profile_d ()
  {
    local file=
    for file in $(export LC_COLLATE=C; echo /etc/profile.d/*.); do
      [ -e "${file}" ] && . "${file}"
    done

    if [ -n ${MINGW_MOUNT_POINT} ]; then
      for file in $(export LC_COLLATE=C; echo ${MINGW_MOUNT_POINT}/etc/profile.d/*.); do
        [ -e "${file}" ] && . "${file}"
      done
    fi
  }

  for postinst in $(export LC_COLLATE=C; echo /etc/post-install/*.post); do
    [ -e "${postinst}" ] && . "${postinst}"
  done

  if [ ! "x${BASH_VERSION}" = "x" ]; then
    HOSTNAME="$(/usr/bin/hostname)"
    profile_d sh
    [ -f "/etc/bash.bashrc" ] && . "/etc/bash.bashrc"
  elif [ ! "x${KSH_VERSION}" = "x" ]; then
    typeset -l HOSTNAME="$(/usr/bin/hostname)"
    profile_d sh
    PS1=$(print '3]0;${PWD}\n3[32m${USER}@${HOSTNAME} 3[33m${PWD/${HOME}/~}3[0m\n$ ')
  elif [ ! "x${ZSH_VERSION}" = "x" ]; then
    HOSTNAME="$(/usr/bin/hostname)"
    profile_d zsh
    PS1='(%n@%m)[%h] %~ %% '
  elif [ ! "x${POSH_VERSION}" = "x" ]; then
    HOSTNAME="$(/usr/bin/hostname)"
    PS1="$ "
  else
    HOSTNAME="$(/usr/bin/hostname)"
    profile_d sh
    PS1="$ "
  fi

  if [ -n "$ACLOCAL_PATH" ]
  then
    export ACLOCAL_PATH
  fi

  export PATH MANPATH INFOPATH PKG_CONFIG_PATH USER TMP TEMP PRINTER HOSTNAME PS1 SHELL tmp temp
  test -n "$TERM" || export TERM=xterm-256color

  if [ "$MAYBE_FIRST_START" = "true" ]; then
    sh /usr/bin/regen-info.sh

    if [ -f "/usr/bin/update-ca-trust" ]
    then
      sh /usr/bin/update-ca-trust
    fi

    clear
    echo
    echo
    echo "###################################################################"
    echo "#                                                                 #"
    echo "#                                                                 #"
    echo "#                   C   A   U   T   I   O   N                     #"
    echo "#                                                                 #"
    echo "#                  This is first start of MSYS2.                  #"
    echo "#       You MUST restart shell to apply necessary actions.        #"
    echo "#                                                                 #"
    echo "#                                                                 #"
    echo "###################################################################"
    echo
    echo
  fi
  unset MAYBE_FIRST_START

回答by VonC

So, $HOMEis what I need to modify.

However I have been unable to find where this mythical $HOMEvariable is set so I assumed it was a Linux system version of PATH or something.

所以,$HOME是我需要修改的。

但是我一直无法找到这个神话$HOME变量的设置位置,所以我假设它是 PATH 的 Linux 系统版本或其他东西。

Git 2.23 (Q3 2019) is quite explicit on how HOMEis set.

Git 2.23(2019 年第三季度)非常明确地说明了如何HOME设置。

See commit e12a955(04 Jul 2019) by Karsten Blees (kblees).
(Merged by Junio C Hamano -- gitster--in commit fc613d2, 19 Jul 2019)

请参阅Karsten Blees ( )提交的 e12a955(2019 年 7 月 4 日(由Junio C Hamano合并-- --提交 fc613d2 中,2019 年 7 月 19 日)kblees
gitster

mingw: initialize HOME on startup

HOMEinitialization was historically duplicated in many different places, including /etc/profile, launch scripts such as git-bash.vbsand gitk.cmd, and (although slightly broken) in the git-wrapper.

Even unrelated projects such as GitExtensionsand TortoiseGitneed to implement the same logic to be able to call git directly.

Initialize HOMEin Git's own startup code so that we can eventually retire all the duplicate initialization code.

mingw:在启动时初始化 HOME

HOME初始化在历史上曾在许多不同的地方重复出现,包括/etc/profilegit-bash.vbs和等启动脚本gitk.cmd,以及git-wrapper.

即使是不相关的项目,例如GitExtensions和 也TortoiseGit需要实现相同的逻辑才能直接调用 git。

HOME在 Git 自己的启动代码中进行初始化,这样我们最终可以淘汰所有重复的初始化代码。

Now, mingw.cincludes the following code:

现在,mingw.c包含以下代码:

/* calculate HOME if not set */
if (!getenv("HOME")) {
    /*
     * try $HOMEDRIVE$HOMEPATH - the home share may be a network
     * location, thus also check if the path exists (i.e. is not
     * disconnected)
     */
    if ((tmp = getenv("HOMEDRIVE"))) {
        struct strbuf buf = STRBUF_INIT;
        strbuf_addstr(&buf, tmp);
        if ((tmp = getenv("HOMEPATH"))) {
            strbuf_addstr(&buf, tmp);
            if (is_directory(buf.buf))
                setenv("HOME", buf.buf, 1);
            else
                tmp = NULL; /* use $USERPROFILE */
        }
        strbuf_release(&buf);
    }
    /* use $USERPROFILE if the home share is not available */
    if (!tmp && (tmp = getenv("USERPROFILE")))
        setenv("HOME", tmp, 1);
}
/* calculate HOME if not set */
if (!getenv("HOME")) {
    /*
     * try $HOMEDRIVE$HOMEPATH - the home share may be a network
     * location, thus also check if the path exists (i.e. is not
     * disconnected)
     */
    if ((tmp = getenv("HOMEDRIVE"))) {
        struct strbuf buf = STRBUF_INIT;
        strbuf_addstr(&buf, tmp);
        if ((tmp = getenv("HOMEPATH"))) {
            strbuf_addstr(&buf, tmp);
            if (is_directory(buf.buf))
                setenv("HOME", buf.buf, 1);
            else
                tmp = NULL; /* use $USERPROFILE */
        }
        strbuf_release(&buf);
    }
    /* use $USERPROFILE if the home share is not available */
    if (!tmp && (tmp = getenv("USERPROFILE")))
        setenv("HOME", tmp, 1);
}

回答by garg10may

I faced exactly the same issue. My home drive mapped to a network drive. Also

我遇到了完全相同的问题。我的主驱动器映射到网络驱动器。还

  1. No Write access to home drive
  2. No write access to Git bash profile
  3. No admin rights to change environment variables from control panel.
  1. 没有对主驱动器的写访问权限
  2. 没有对 Git bash 配置文件的写访问权限
  3. 没有从控制面板更改环境变量的管理员权限。

However below worked from command line and I was able to add HOMEto environment variables.

然而,下面是从命令行工作的,我能够添加HOME到环境变量中。

rundll32 sysdm.cpl,EditEnvironmentVariables

rundll32 sysdm.cpl,EditEnvironmentVariables

回答by grsiepka

I know this is an old question, but it is the top google result for "gitbash homedir windows" so figured I'd add my findings.

我知道这是一个老问题,但它是“gitbash homedir windows”的顶级谷歌结果,所以我想我会添加我的发现。

No matter what I tried I couldn't get git-bash to start in anywhere but my network drive,(U:) in my case making every operation take 15-20 seconds to respond. (Remote employee on VPN, network drive hosted on the other side of the country)

无论我尝试什么,我都无法让 git-bash 在我的网络驱动器之外的任何地方启动,(U:) 在我的情况下,每个操作都需要 15-20 秒才能响应。(VPN 上的远程员工,该国另一端托管的网络驱动器)

I tried setting HOME and HOMEDIR variables in windows.

我尝试在 Windows 中设置 HOME 和 HOMEDIR 变量。

I tried setting HOME and HOMEDIR variables in the git installation'setc/profile file.

我尝试在 git 安装的 sett/profile 文件中设置 HOME 和 HOMEDIR 变量。

I tried editing the "Start in" on the git-bash shortcut to be C:/user/myusername.

我尝试将 git-bash 快捷方式上的“Start in”编辑为 C:/user/myusername。

"env" command inside the git-bash shell would show correct c:/user/myusername. But git-bash would still start in U:

git-bash shell 中的“env”命令将显示正确的 c:/user/myusername。但是 git-bash 仍然会以 U 开头:

What ultimately fixed it for me was editing the git-bash shortcut and removing the "--cd-to-home" from the Target line.

最终为我修复的是编辑 git-bash 快捷方式并从目标行中删除“--cd-to-home”。

I'm on Windows 10 running latest version of Git-for-windows 2.22.0.

我在 Windows 10 上运行最新版本的 Git-for-windows 2.22.0。

回答by Oz Bar-Shalom

Here you go: Here you go: Create a System Restore Point. Log on under an admin account. Delete the folder C:\SomeUser. Move the folder c:\Users\SomeUser so that it becomes c:\SomeUser. Open the registry editor. Navigate to HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList. Search for "ProfileImagePath" until you find the one that points at c:\Users\SomeUser. Modify it so that it points at c:\SomeUser. Use System Restore in case things go wrong.

给你:给你:创建系统还原点。在管理员帐户下登录。删除文件夹 C:\SomeUser。移动文件夹 c:\Users\SomeUser,使其变为 c:\SomeUser。打开注册表编辑器。导航到 HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList。搜索“ProfileImagePath”,直到找到指向 c:\Users\SomeUser 的那个。修改它,使其指向 c:\SomeUser。如果出现问题,请使用系统还原。