I don’t know why this gave me so much trouble, but it did. When I installed Git for Windows, the default, Bash-like Windows terminal for git, it insisted that its home directory was /s (nowhere near my Documents folder in windows).

Here is a quick tip to make Git Bash change its home directory automatically (so you don’t have to cd /blah/blah/blah every time you open it).

Open your Git for Windows client. Use vim to create a .bashrc file (this file gets automatically run every time the terminal is open):

vim .bashrc

Press ‘i’ to go into ‘Insert’ mode. Type in the following (change <my/desired/home/dir> to your new home directory).

moveHome() { cd /c/<my/desired/home/dir>; }
moveHome
export HOME=/c/<my/desired/home/dir>

Your .bashrc should look something like this:

Adding .bashrc to Git Bash

Press ‘esc’ to exit “Insert” mode. Type “:wq” and press ‘enter’ to save and exit. Now, reload your Git terminal window, and you should be in a new home directory!

EDIT (12/9/14): Added export line to .bashrc file to update environment variable.

20 thoughts on “Quick Tip: Changing Default Directory of Git Bash

  1. Igor on June 8, 2016 at 5:42 pm Reply

    didn’t work for me =/

    1. ShawnHymel on June 10, 2016 at 7:22 pm Reply

      Sorry to hear that. Did you make sure the home directory was typed out exactly as you intended (e.g. double check it in something like Windows Explorer)? I’ve done this trick a few times now, and it still works for me.

  2. Jordan on June 22, 2016 at 12:03 am Reply

    didn’t work for me either.
    Now it shows line 1: syntax error near unexpected token `{cd’

    1. ShawnHymel on June 23, 2016 at 3:02 am Reply

      The space between { and ‘cd …’ is necessary. For some reason, the script won’t work without it.

  3. Yeshua Torreblanca on July 19, 2016 at 9:31 pm Reply

    It worked for me fine. Windows 10.

  4. Vlas on July 22, 2016 at 9:24 am Reply

    Good, thanks! Really helpful. But anyway, it is not necessary to use vim 🙂

    1. ShawnHymel on July 22, 2016 at 2:54 pm Reply

      True, but easy enough to do from within Git Bash 🙂

  5. The Bro on October 11, 2016 at 10:23 pm Reply

    Worked like a champ!

  6. Jacques on March 24, 2017 at 5:24 pm Reply

    Thanks, it worked fine.

  7. Roen on April 7, 2018 at 9:22 am Reply

    Thanks! It worked.

    1. ShawnHymel on April 9, 2018 at 3:30 pm Reply

      Glad it helped!

  8. D on April 10, 2018 at 9:47 pm Reply

    I’m late to the game, but why not:

    export HOME=/c/
    cd

    1. ShawnHymel on May 23, 2018 at 12:59 am Reply

      I suppose that will work, too. More than one way to skin a cat 🙂

  9. B on June 2, 2018 at 3:34 am Reply

    Worked for me on Windows10. Thanks a ton! Cheers!

  10. M on June 12, 2018 at 5:24 pm Reply

    Worked on Win10, thanks!

  11. M on June 6, 2019 at 3:43 pm Reply

    Did not work for me…

    1. ShawnHymel on June 7, 2019 at 1:59 pm Reply

      Sorry to hear that. Did you make sure the home directory was typed out exactly as you intended (e.g. double check it in something like Windows Explorer)? I’ve done this trick a few times now, and it still works for me.

  12. Charles on September 13, 2019 at 5:29 pm Reply

    Wonder is have a space in the file name will keep this from working? Where would you put the quotation marks?

    1. ShawnHymel on September 16, 2019 at 3:44 pm Reply

      To work with spaces in the path, you need to enclose the whole path in single (‘) or double (“) quotation marks:

      ‘/c/Program Files/Something’
      “/c/Program Files/Something”

      You can also use the backslash (\) character to escape the space character:

      /c/Program\ Files/Something

  13. daeta on January 22, 2023 at 2:22 pm Reply

    I misunderstood your final Vim command. For posterity (and those with high DPI monitors) that’s a colon “:” followed by “wq”. Than

Leave a Comment Cancel Comment

Your email address will not be published. Marked fields are required.