Git Bash is an application that provides Git command line features for Windows, simulating a Bash environment. When using Git Bash, you may find the need to change your default home directory, the default starting directory that Git Bash opens in upon launch. Changing the home directory can make work more efficient by reducing the need to navigate through multiple directories each time Git Bash is opened. This guide will provide detailed steps on how to change the home directory in Git Bash.
Understanding the Git Bash home directory
In Git Bash, the home directory is where Git Bash starts upon launch. This directory is significant because it usually contains user-specific configuration files like .bashrc
, .gitconfig
, and others. By default, Git Bash sets the home directory to the user’s profile folder (C:\Users\<username>
).
How to change the home directory in Git Bash
Method 1: Using Windows environment variables
One of the most straightforward methods to change the home directory for Git Bash is by setting or modifying the Windows $HOME
environment variable.
Steps:
Open system properties:
- Right-click on the Start menu, select System, then click on Advanced system settings on the left sidebar.
- Go to the Advanced tab and click on Environment Variables.
Set HOME variable:
- Under the User variables section, click on New... to create a new environment variable.
- Set
Variable name
asHOME
andVariable value
as the path to your new desired directory, e.g.,C:\GitHome
.
Restart Git Bash:
- Close and reopen Git Bash to allow the change to take effect. To verify that Git Bash now opens to the correct default directory, you can use the command
pwd
to print the directory you're currently in. This should be the directory you set in the previous steps.
- Close and reopen Git Bash to allow the change to take effect. To verify that Git Bash now opens to the correct default directory, you can use the command
Method 2: Modifying the shortcut target
Another method to change the default starting directory for Git Bash involves modifying the properties of the Git Bash shortcut.
Steps:
Right-click on the Git Bash shortcut:
- Find the Git Bash shortcut, usually on your desktop or start menu.
- Right-click on it and select Properties.
Modify the start in field:
- In the Shortcut tab, locate the Start in field.
- Change the path in the Start in box to your desired default directory, e.g.,
C:\GitHome
.
Save and test:
- Click Apply, then OK to save your changes.
- Open Git Bash using this shortcut to start directly in the new default directory.
Method 3: Using a bash configuration file
For users who prefer a scripting approach, you can dynamically set the home directory each time Git Bash starts by editing the .bashrc
file.
Steps:
Open
.bashrc
File:- In Git Bash, type
nano ~/.bashrc
to open the Bash run commands file in nano text editor.
- In Git Bash, type
Add script to change home directory:
- Add the following line at the end of the file:Terminalexport HOME="/d/GitHome"cd $HOME
- Replace
/d/GitHome
with your desired directory path.
- Add the following line at the end of the file:
Save and exit:
- Press Ctrl+O, Enter, and then Ctrl+X to save and exit nano.
Source
.bashrc
or restart Git Bash:- Type
source ~/.bashrc
or restart Git Bash to apply the changes.
- Type
For more info on configuring Git Bash, see the official documentation.