How to Set the Default Directory for Git Bash on Windows
Setting the default directory for Git Bash can be very helpful for a hassle-free start to your development for the day.
Method 1: Change in Shortcut
When we open Git Bash, we’re running git-bash.exe
.
We don’t want to mess with this file.
In order to set the default directory, we need to find the shortcut to git-bash.exe
.
When I search git bash
in the Start Menu and Open File Location
of the shortcut, it brings me to C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Git
.
Once we find our shortcut, we can right click
the Git Bash shortcut and select Properties
.
Inside Target:
, we have "C:\Program Files\Git\git-bash.exe" --cd-to-home
. We want to remove the --cd-to-home
flag.
Then we want to set our desired default directory inside Start in:
.
Method 2: Change in ~/.bashrc
We can also modify our ~/.bashrc
file to change the default directory.
All we have to do is add this line to our ~/.bashrc
.
cd C:/path/to/folder;
I’ll walk you through how to add this line to your ~/.bashrc
.
Append to ~/.bashrc
using >>
One way to do this is to simply echo the cd
command and append that line into ~/.bashrc
.
echo "cd C:/path/to/folder;" >> ~/.bashrc
Angle brackets redirect output and allow us to dump the line into an alternate location (i.e. a file). Double angle brackets (>>
) append to an existing file while a single angle bracket (>
) will overwrite the file.
Append to ~/.bashrc
using vim
We can also use vim
inside Git Bash to create/edit this file.
- Open up Git Bash
- Run
vim ~/.bashrc
- Press
i
in order to insert characters - Type
cd C:/path/to/folder;
(make sure it’s on its own line) - Press
esc
to exit the insert mode - Press
:wq
orZZ
to save and exitvim
You’ve successfully updated ~/.bashrc
! Type cat ~/.bashrc
to double check.