簡體   English   中英

在 Windows 安裝中更改 ~ 目錄的位置 Git Bash

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

我什至不確定我在問正確的問題。 說說我的情況:

這大約是 Windows 7 上的 Git。

我公司在 .network 驅動器上設置 Windows 用戶目錄,而不是在本地硬盤驅動器上(用於備份和其他目的,超出本問題的 scope)。 我不能改變那個政策。

然而,我可以在該方案之外擁有本地文件,這就是我的 Apache 服務器的設置方式。 完全是本地的。

我安裝了 Git。它安裝了 Bash。當我啟動 Bash 和cd ~ (切換到根目錄)時,我發現它(Bash 根目錄)指向 my.network 用戶目錄。 我希望它不要那樣做。 我希望~位於我硬盤上的其他地方。 我想要它,以便當我執行其他操作(例如安裝證書、SSH 密鑰等)時,它的默認值不會自動使用 that.network 目錄。

到處都找不着,能找到的都是涉及別名、重定向、$HOME指向的位置的答案。 但這根本不是我想要的。

問題是:我可以更改那個目錄嗎? 如果是這樣:如何?

更新:所以,$HOME 是我需要修改的。 但是我一直無法找到這個神秘的 $HOME 變量的設置位置,所以我假設它是 Linux 系統版本的 PATH 之類的。 無論如何...

我在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

我不明白,為什么您不想設置$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

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

變量名稱: HOME
變量值: %USERPROFILE%

如何設置環境變量(如果您不是系統管理員,則可以使用用戶名部分的用戶變量

我會分享我所做的,這不僅適用於 Git,也適用於 MSYS/MinGW。

HOME環境變量通常不會為 Windows 應用程序設置,因此通過 Windows 創建它不會影響任何其他內容。 從計算機屬性(在資源管理器中右鍵單擊計算機 - 或任何名稱,然后選擇屬性,或控制面板 -> 系統和安全 -> 系統),選擇Advanced system settings ,然后選擇Environment Variables...並創建一個新的HOME ,然后將其分配到您喜歡的任何位置。

如果您不能創建新的環境變量,其他答案仍然有效。 (我詳細介紹了如何精確地創建環境變量,因為它很難找到。)

您可以在默認$HOME目錄(例如C:\Users\WhateverUser\.bash_profile )中創建具有以下內容的.bash_profile而不是修改全局profile

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

1.右鍵單擊Gitbash快捷方式選擇屬性
2.選擇“快捷方式”選項卡
3.將您的起始目錄鍵入“開始於”字段
4.從“目標”字段中刪除“--cd-to-home”部分

所以,$HOME 是我需要修改的。 然而,我一直無法找到這個神話般的 $HOME 變量的設置位置,所以我假設它是 PATH 或其他東西的 Linux 系統版本。 無論如何...**

回答

profile文件的頂部添加 HOME 有效。

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

我知道這是一個老問題,但它是“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 快捷方式並從目標行中刪除“--cd-to-home”。

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

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

然而,我一直無法找到這個神秘的$HOME變量的設置位置,所以我假設它是 PATH 或其他的 Linux 系統版本。

Git 2.23(2019 年第 3 季度)對於HOME的設置方式非常明確。

請參閱Karsten Blees ( kblees )提交 e12a955 (2019 年 7 月 4 日)。
(由Junio C Hamano 合並gitster 提交 fc613d2,2019年 7 月 19 日)

mingw:在啟動時初始化 HOME

HOME初始化歷史上在許多不同的地方重復,包括/etc/profile ,啟動腳本,如git-bash.vbsgitk.cmd ,以及(雖然略有損壞)在git-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 (ie; 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); }

我遇到了完全相同的問題。 我的主驅動器映射到網絡驅動器。

  1. 沒有對主驅動器的寫訪問權限
  2. 沒有對 Git bash 配置文件的寫入權限
  3. 沒有從控制面板更改環境變量的管理員權限。

但是下面是從命令行工作的,我能夠將HOME添加到環境變量中。

rundll32 sysdm.cpl,EditEnvironmentVariables

同樣的: $HOME for git bash on Windows 10 被設置為 a.network drive(響應速度慢)。 我沒有更改系統環境變量、Git 配置文件等的權限。我可以為我的用戶添加 HOME 變量:

  • 在開始搜索“環境”
  • go 到“為您的帳戶編輯環境變量”(不是“編輯系統環境變量”)
  • 在“環境變量”window,“XYZXYZ 的用戶變量”-->“新建”
  • 在“新用戶變量”中-->
    • “變量名:”設置為“HOME”
    • “變量值:”設置為“C:\Users\XYZXYZ”(或任何你需要的),點擊“確定”
  • 重新啟動(或關閉/打開) git bash ,測試ls ~

給你:給你:創建系統還原點。 使用管理員帳戶登錄。 刪除文件夾 C:\SomeUser。 移動文件夾 c:\Users\SomeUser,使其成為 c:\SomeUser。 打開注冊表編輯器。 導航到 HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList。 搜索“ProfileImagePath”,直到找到指向 c:\Users\SomeUser 的路徑。 修改它,使其指向 c:\SomeUser。 萬一出現問題,請使用系統還原。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM