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

13

183

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

This is about Git on Windows 7.

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.

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

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.

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.

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...

I do have a "profile" file under git/etc. Here are the contents (notice no $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
Generation answered 26/8, 2015 at 17:27 Comment(8)
~ represents your home directory. Changing the HOME environment variable will change where it goes.Katydid
Just a note: HOME environment variable doesn't change only where ~ points to, but also everything what's accessing the user's home directory should go there.Darsey
~ is just a shorthand for "user's home directory", so wherever ~ points to, there is the user's home directory.Darsey
Yea, I cannot find this $HOME variable to set it. Above I added what my profile file looks like.Generation
I see, you probably have a different version of Git. What version do you have? BTW try to add setting of HOME to the end of the profile. But it's possible, that it will be overwritten somewhere else.Darsey
Just a note to your update: HOME is not a Linux version of PATH. Linux uses PATH very similarly as Windows. HOME is equivalent of HOMEDRIVE + HOMEPATH on Windows, you just have it in one variable on Linux (since there aren't that stupid drive letters). On both systems, HOME (or Windows equivalent) is just a pointer to the user's (profile) directory and PATH is list of directories to be searched for executable when it's executed (without specifying a path explicitly).Darsey
Please don't post your answer in your question. I'm going to move it to a community-wiki answer. Feel free to post your own and I'll delete mine. I've rolled your question back to the version without the answer.Horodko
Possible duplicate of How do I change the default location for Git Bash on Windows?My
L
190

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

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

Quick'n'dirty solution

Edit C:\Program Files (x86)\Git\etc\profile and set $HOME variable 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:

HOME=/c/my/custom/home

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

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

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

HOME=/c/Users/$USERNAME

Cleaner solution

Set the environment variable HOME in 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.

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:

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
Lugsail answered 26/8, 2015 at 18:6 Comment(15)
Thanks, I cannot find a $HOME variable in my profile file. I've added to the question. Please take a look. maybe I need to look elsewhere for that variable?Generation
OK, it's hard to say what's wrong, in that case. Try to search whole C:\Program Files (x86)\Git` directory for HOME`. That variable has to be set somewhere.Darsey
No other file in the entire directory calls/sets $HOME.Generation
So did you try to just add it anywhere? For example to the bottom of profile? Just a note that content of HOME variable should look like this in git-bash: /c/some/path.Darsey
BTW does HOME variable exists in git-bash's shell (before you add it anywhere)?Darsey
adding HOME at the top of the profile file worked.HOME="c://path/to/custom/root/"Generation
Awesome, I'm glad you solved your problem. It's possible that it wasn't set at all before and in that case the shell sets it (automagically) to a Windows user home directory.Darsey
Thanks for your comment @HawkeyeParker but the question is related to Windows platform so it's a bit specific. There's no file environment under the etc directory inside the Git's installation directory. I have tried to create it and put some assignment there but it's being ignored.Darsey
Adding 'home' system environment variable solved the issue rather than editing the profile file in git folderPhosgene
Setting HOME="$(cygpath -u "$USERPROFILE")" solved the half of my problem. Now env $home ans "~" is correct. but git-bash.exe still tries to load and save itf config under c:\windows\system32. So is there anything else I must change that git-bash knows where it should save and load?Amalamalbena
Setting HOME for all users like HOME=c:\custom\home\%USERNAME% does not work on my Windows 10 machines and probably not on any of them. Why? Because the USERNAME variable on system level is already set to SYSTEM. And that is what will get used and not your current logged in username. Setting it on user level explicitly does however seem to work. In fact that also solves another Git issue that Git for Windows uses wrong env variables to determine home which then ends of on some network drive on corporates environments.Williwaw
I like this, and wouldn't call the first solution "quick and dirty", it is exactly what the OP asked for. For my uses, I "only" want git to open a specific folder and it typically changes when my projects change, and no other applications. I do not recommend changing environment variables for specific applications because it could have unforeseen consequences with other applications.Chemise
For anyone wondering, here is how to Create and Modify Environment Variables on WindowsIntaglio
For me, the Git Bash /etc/profile file was located here: C:\Users\my_username\AppData\Local\Programs\Git\etc\profile. So, I added HOME="/c/Users/$(whoami)" to the very top of it. See my full answer and details here. I also talk a bit about Windows and Linux paths in Git Bash and MSYS2 there.Intaglio
Yes, I second that. Please add a short information and a link (e.g., from the comments) on how to modify environment variables on Windows (pertinent to "Cleaner solution"). Thanks.Befoul
M
77

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

Variable name: HOME
Variable value: %USERPROFILE%

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

Maryn answered 23/10, 2017 at 21:29 Comment(5)
This worked perfectly for me, and is a better solution IMO to the accepted answer.Alpha
This answer should go to the top.Hinkley
This worked but not when using multiple users on the same machine. I had to go with adding HOME=/c/Users/$USERNAME to profile.Refractory
This should be the top answer. It's the only way (only way I could find) to get GitBash to use the .ssh directory in my Windows user folder (C:\Users\UserName). On my system it was defaulting to %HOMEDRIVE% (M:). And of course it will still work after and update to GitBash!Fond
This works if you have no $HOME variable set, but it sounds like the OP has $HOME set by company policy. I have his problem, too. This answer doesn't work in that case (the new setting gets automatically overwritten by company policy).Febrifuge
M
43

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

The HOME environment 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.

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.)

Magenta answered 11/1, 2016 at 21:33 Comment(10)
If you don't have admin access to change the environment from the control panel, you will need to issue the following command, rundll32 sysdm.cpl,EditEnvironmentVariables, from the command prompt, or type environment in the start menu to launch the same command.Gwenny
This worked for me for setting the home directory for git-bash. Thanks for the re-assurance that it's OK to set the HOME environment variable in Windows, and also detailed instructions on how to do it. One simple change to accomplish my goal.Adelina
%USERPROFILE% is a good value to your HOME environment variable. See my answerMaryn
@Maryn That's a possibility, and depending on your needs, maybe a good idea. For myself, I specifically did not to do it ithat way. Partly to keep the Windows and Unix-like environments separate (to avoid clutter), and partly to have my HOME (~) closer to root / tree. I actually created a directory C:/home and use that. Also,BTW, the OP noted that for him, his %USERPROFILE% is on a network drive, and awfully painful to use as you described.Magenta
I used this to solve an issue with Git. For me it made a difference to add a Uservariable HOME of value %USERPROFILE%, it did not help to add a Systemvariable of same name and value.Beauchamp
One additional comment I would put: The HOME variable should be set in the Windows format instead of the UNIX format. So "C:\Users\me" instead of "/c/Users/me". I thought it would be the latter since it is being read by git bash which uses UNIX formatCircumnavigate
@Circumnavigate Actually, on my system I set the value by Browseing to the desired directory. For environment variables set within Git Bash to be read by Windows programs, you may want to use the polyglot ("mixed") format, C:/Users/name - the drive letter, but forward slashes.Magenta
This is a better solution than the accepted one -- Adding 'HOME' system environment variable solved the issue rather than editing the profile file in git folder.Colloquialism
@Colloquialism Minor nitpick: Your edit implies that my solution was developed for Git Bash, and also works on MSYS and MinGW. In fact, it was the other way around. BTW, the accepted answer does reference system environment variables as the "Cleaner solution", though it doesn't specify how to create it or emphasize that it's a Windows feature.Magenta
Yes Menachem, I appreciate your being precise, but what I wanted to achieve was to simply the process details but stress on the result, which has been confirmed by many people including Stephen Hosking and me. I.e., to me the end result matters, the other detailed are more a distraction. Hope that's OK with you.Colloquialism
C
25

Instead of modifying the global profile you could create the .bash_profile in your default $HOME directory (e.g. C:\Users\WhateverUser\.bash_profile) with the following contents:

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
Condign answered 14/6, 2016 at 10:14 Comment(2)
Yes! .bash_profile is the correct way to customise your environment, hacking .profile is not good practise.Shaitan
@ropata "hacking" ~/.profile is fine. In fact, the docs prefer it over ~/.bash_profie: "Suitable files for environment variables that affect just a particular user (rather than the system as a whole) are...~/.profile." And: "Shell config files such as...~/.bash_profile...are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session." help.ubuntu.com/community/EnvironmentVariablesIsallobar
C
10

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

Chrotoem answered 13/2, 2017 at 7:22 Comment(5)
doesn't actually change the $HOME variableShaitan
What version of Windows? It does not seem to be possible to get to Properties in Windows 10.Clifton
@PeterMortensen right click to Gitbash SHORTCUT choose PropertiesMelinamelinda
ugh, this was all i needed, thanks. the shortcut was going into the Git program folder and .bash_profile/.bashrc there had no effect. finally just set the Start In folder to my home directory and everything's fine. this is all something crazy related to my company's home directory assignment, like the original poster says.Mother
for me it worked both answers of @ÖzgürÖztürk and @dekkard combined. It seems that removing --cd-to-home let git searching in the Your_username for the .batch_profile.Oblast
H
5

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...**

Answer

Adding HOME at the top of the profile file worked.

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
Horodko answered 26/8, 2015 at 17:27 Comment(0)
K
4

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.

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)

I tried setting HOME and HOMEDIR variables in windows.

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

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

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

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

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

Kraal answered 19/7, 2019 at 17:34 Comment(2)
Hi grsiepka - thank you for your first contribution to SO. Reading your answer (I'm reviewing it) what jumps out is that it isn't so much as an answer as well as a description of what you did all try that didn't work ;). Imagine other users looking for an answer and finding this question - then they expect/want the (accepted) answer to tell them how to fix their problem. A good answer is written with this in mind. Can you edit your answer and make it shorter? Thanks.Hydranth
None of the "accepted" answers worked for me. I posted what did. If that's not helpful feel free to remove my answer.Kraal
S
4

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.

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

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

mingw: initialize HOME on startup

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

Even unrelated projects such as GitExtensions and TortoiseGit need to implement the same logic to be able to call git directly.

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

Now, mingw.c includes the following code:

/* 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);
}
Same answered 21/7, 2019 at 3:39 Comment(0)
S
1

The same: $HOME for git bash on Windows 10 was set to a network drive (slow responsiveness). I do not have permissions to change system environment variables, Git profile, etc. I could add HOME variable for my user:

  • in Start search for "Environment"
  • go to "Edit environment variables for your account" (not "Edit the system environment variables")
  • in "Environment Variables" window, "User variables for XYZXYZ" --> "New"
  • in "New User Variable" -->
    • "Variable name:" set to "HOME"
    • "Variable value:" set to "C:\Users\XYZXYZ" (or whatever you need), click "Ok"
  • restart (or close/open) git bash, test ls ~
Syllogize answered 11/1, 2023 at 18:41 Comment(0)
I
1

Quick summary

Add the following to the very top of your C:\Users\my_username\AppData\Local\Programs\Git\etc\profile file:

HOME="/c/Users/$(whoami)"

Note: if you don't have that file, try this file: C:\Program Files\Git\etc\profile. If you don't have that file, open Git Bash and run the following (I mention this again further below):

cd /
explorer .

Then find your etc\profile file at the location in which Windows Explorer opens up, and edit that file.

Restart all instances of Git Bash.

Adding HOME="/c/Users/$(whoami)" to the top of your Git Bash profile file makes your HOME (~) directory get set to C:\Users\my_username. Forcing this manually may be necessary if you have a networked or work computer, as otherwise your HOME directory may point to a network drive, which can 1) make Git Bash run really slowly, and 2) prevent your custom C:\Users\my_username\.bashrc file from being sourced properly.

From @Paul Wintz's answer here: Git Bash (mintty) is extremely slow on Windows 10 OS:

Per @Alexandre Jobin's comment, Git Bash can be very slow if your HOME variable points to a shared network folder.

Details

I like to set the HOME (~) directory by setting it at the lowest level possible in the Git Bash system: namely, in the profile file which Bash sources (imports, or runs) as it opens up, rather than messing with Windows Environment variables or other things.

So, here's how I do it:

Set your HOME directory in Git Bash

Open up the C:\Users\my_username\AppData\Local\Programs\Git\etc\profile file in a text editor, such as Microsoft VSCode.

Add this to the very top of the file:

# CUSTOM CHANGE: change your home (`~`) dir to `C:\Users\my_username`.
# - Or, set it to whatever else you want here.
HOME="/c/Users/$(whoami)"
# OR (same thing)
# HOME="/c/Users/$USERNAME"

If you want a different HOME directory than that, customize it to your liking, of course.

Close and re-open all Git Bash terminals to see the changes take effect.

Confirm the change by running echo "$HOME" and echo ~ in Git Bash, which should now show your new HOME directory. I see /c/Users/gabriel, for example.

Done!

How did I find the path to the C:\Users\my_username\AppData\Local\Programs\Git\etc\profile file?

I opened Git Bash and ran this:

# navigate to what Git Bash has declared the "root" of this filesystem;
# since we are in Windows, I know this is a fake root
cd /

# Now, force Windows Explorer to open up in this directory so I can see 
# where it is!
explorer .

Voila! Windows Explorer opened up in C:\Users\gabriel\AppData\Local\Programs\Git. Now, I know from experience on Linux Ubuntu that one of the core files loaded in Bash is the profile file located at /etc/profile. So, I looked in Windows Explorer there and sure enough, there's an etc/ directory there and a profile file inside it. So, I tried setting the HOME directory there, and it worked!

More details on paths in Git Bash and MSYS2

Above, I am setting your HOME directory to be C:\Users\my_username, where it cleverly uses the whoami command to get your username, so that everyone can use the same code above regardless of your username, since it will be automatically filled in for you.

Note that since Git Bash is based on the MSYS2 MINGW64 environment, which is a native Windows environment which gives you access to Linux-like tools and things, you can use a variety of path styles in the file above, including the following. I have personally tested each of these to work:

# Unix-style paths (my preference)
HOME="/c/Users/$(whoami)"

# Windows-style paths
# NOTE: You must escape the 2nd backslash in this case with another backslash 
# or else it mistakenly escapes the next character (`$`)
HOME="C:\Users\\$(whoami)"

# mixed paths (using both / and \), again escaping the backslash before 
# the $ sign
HOME="/c\Users\\$(whoami)"
# OR
HOME="C:/Users\\$(whoami)"
# OR
HOME="C:\Users/$(whoami)"

# funky weirdness paths with extra slashes
HOME="///C:\\Users\\///\\$(whoami)"

# etc.

Beware that multiple forward slashes (/) in a row is fine. But, backslashes (\) are escape characters in Unix/Linux, so they must generally come in pairs. Therefore, this does not work: HOME="C:////\\Users\\///\\\$(whoami)". The reason is that the \\\$ part near the end is invalid. The first two backslashes there are fine because the first one escapes the second one, but then the \$ part is an escaped dollar sign $, which breaks the command substitution $(whoami).

I prefer to use the Unix-style paths, like this: HOME="/c/Users/$(whoami)", wherever possible.

Paths in Git Bash and MSYS2 can be very confusing at times, because the MSYS2 and Git Bash terminals automatically do path conversion from Unix-style paths to Windows-style paths for you. But, sometimes there are nuances with how absolute paths are handled. Take my problem, for instance, where I lost many days of debugging: the question: checking size of mp_limb_t... 0 and configure: error: Oops, mp_limb_t doesn't seem to work when compiling the Microchip XC32 compiler from source. My answer: The fix: when compiling in an MSYS2 terminal in Windows, absolute #include paths are not allowed in C and C++!. In short, I had to use relative, not absolute, paths.

Read more about how MSYS2 and Windows vs Unix paths work, here:

  1. https://www.msys2.org/docs/filesystem-paths/
  2. https://github.com/msys2/MINGW-packages/issues/6711#issuecomment-1797982823

See also

  1. Here are my full instructions for setting up MSYS2: Installing & setting up MSYS2 from scratch, including adding all 7 profiles to Windows Terminal. I do something similar to set the HOME directory there, too.
  2. My general instructions & screenshots: Installing Git For Windows
  3. My answer: Python doesn't work in Git Bash (it just hangs or freezes forever); and getting Linux hash-bangs to work in Windows
Intaglio answered 9/11, 2023 at 2:38 Comment(0)
D
0

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.

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

rundll32 sysdm.cpl,EditEnvironmentVariables

Dieball answered 28/1, 2019 at 8:47 Comment(1)
This is just a console shortcut to access the environment variable editorUnfriended
G
0

Based on the answer https://mcmap.net/q/12595/-change-the-location-of-the-directory-in-a-windows-install-of-git-bash I would like to supplement it suddenly will be useful.

We have GPOs set up in our company, and therefore, no matter what changes we make, they will be replaced by group policies. HOME folder, for example T:, is configured for all users as a DFS-based network share (most likely), then to create files in the root of the T: drive is not present, and when trying to clone a repository or to create a user .gitconfg, an error occurs. I reconfigured the HOME variable in the git/etc/profile file as follows:

HOME=/c/users/${USERNAME}

And to perform operations, like clone, I use git bash, because in Windows CMD still "HOME=T:/"

Golfer answered 8/6, 2023 at 12:13 Comment(0)
G
-9

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.

Grigsby answered 26/8, 2015 at 17:56 Comment(2)
This will move his Windows user profile. I don't think that's what he want.Darsey
And very possibly, under a corporate setup, he might not have permission for this.Magenta

© 2022 - 2024 — McMap. All rights reserved.