168

我什至不确定我问的是正确的问题。先说明一下我的情况:

这是关于 Windows 7 上的 Git。

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

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

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

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

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

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

我确实有一个“个人资料”文件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 ()
  {
    (( $1 & 0x0002 )) && echo -n "binary" || echo -n "text"
    (( $1 & 0x0010 )) && echo -n ",exec"
    (( $1 & 0x0040 )) && echo -n ",cygexec"
    (( $1 & 0x0100 )) && echo -n ",notexec"
  }

  # Shell dependent settings
  profile_d ()
  {
    local file=
    for file in $(export LC_COLLATE=C; echo /etc/profile.d/*.$1); 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/*.$1); 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 '\033]0;${PWD}\n\033[32m${USER}@${HOSTNAME} \033[33m${PWD/${HOME}/~}\033[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
4

10 回答 10

175

我不明白,为什么您不想设置$HOME环境变量,因为这完全可以解决您的要求。

cd ~不是切换到根目录,而是切换到用户的主目录,由$HOME环境变量设置。

Quick'n'dirty 解决方案

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

HOME=/c/my/custom/home

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

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

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

HOME=/c/Users/$USERNAME

更清洁的解决方案

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

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

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

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

HOME=c:\my\custom\home
于 2015-08-26T18:06:30.680 回答
62

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

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

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

于 2017-10-23T21:29:53.763 回答
40

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

通常HOME不会为 Windows 应用程序设置环境变量,因此通过 Windows 创建它不会影响其他任何内容。从计算机属性(在资源管理器中右键单击计算机 - 或任何名称 - 并选择属性,或控制面板 -> 系统和安全 -> 系统),选择Advanced system settings,然后Environment Variables...创建一个新的HOME, 并分配它任何你喜欢的地方。

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

于 2016-01-11T21:33:28.847 回答
25

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
于 2016-06-14T10:14:35.520 回答
9

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

于 2017-02-13T07:22:26.057 回答
4

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

回答

在文件顶部添加 HOMEprofile有效。

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 ()
  {
    (( $1 & 0x0002 )) && echo -n "binary" || echo -n "text"
    (( $1 & 0x0010 )) && echo -n ",exec"
    (( $1 & 0x0040 )) && echo -n ",cygexec"
    (( $1 & 0x0100 )) && echo -n ",notexec"
  }

  # Shell dependent settings
  profile_d ()
  {
    local file=
    for file in $(export LC_COLLATE=C; echo /etc/profile.d/*.$1); 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/*.$1); 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 '\033]0;${PWD}\n\033[32m${USER}@${HOSTNAME} \033[33m${PWD/${HOME}/~}\033[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
于 2018-02-26T15:18:18.433 回答
3

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

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

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

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

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

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

最终为我解决的问题是编辑 git-bash 快捷方式并从 Target 行中删除“--cd-to-home”。

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

于 2019-07-19T17:34:49.727 回答
3

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

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

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

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

mingw:在启动时初始化 HOME

HOME历史上,初始化在许多不同的地方都有重复,包括/etc/profile启动脚本,例如git-bash.vbsandgitk.cmdgit-wrapper.

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

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

现在,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);
}
于 2019-07-21T03:39:14.473 回答
0

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

  1. 没有对家庭驱动器的写入权限
  2. 没有对 Git bash 配置文件的写入权限
  3. 没有从控制面板更改环境变量的管理员权限。

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

rundll32 sysdm.cpl,EditEnvironmentVariables

于 2019-01-28T08:47:03.997 回答
-7

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

于 2015-08-26T17:56:26.090 回答