3
Back to the intuitive easy to learn Git Bash.
By default Git wants to use your user directory as its home directory. This is where it wants to put all its configuration files and repositories; it is the default location that is opened when you start Git Bash.
On my system it’s here:
C:\Users\Michael Gledhill\
Now I don’t want to use my user directory as the main location for Git, I want to use a special folder that I’ve set up to hold the Git repositories (Git repositories are where we store a software project). I actually want it to use this location:
D:\2500 Git Projects
This is just a local folder on my D drive (see § 2.2.1).
Each repository will be a sub directory of this parent directory (2500 Git Projects).
If your perfectly happy using your user directory as the default directory for Git, you can skip the next bit.
There’s two parts to this:
Changing Git so it stores its configuration files in the new location
Changing Git so it starts in the new location
Taking these in turn:
Git has several configuration files:
It has a master configuration file called gitconfig. On a Windows machine it lives here:
C:\Program Files\Git\mingw64\etc\gitconfig
This is sometimes referred to as the system configuration file.
There is a global configuration file called .gitconfig (note the leading full stop). This is the one that gets used most and is the one we want to move. By default, on a Windows machine, it is located in:
C:\Users\<username>\.gitconfig
Finally, each repository has its own local configuration file called config. This lives in the individual repository:
...\<repositoryname>\.git\config"
But this is inside the .git folder and we don’t go there (see § 2.2.1).
The order in which Git Bash applies these configuration files is:
Master (system) configuration (gitconfig)
Global configuration (.gitconfig)
Local configuration (config)
Effectively, the local configuration has priority because it is executed last and anything in there will override the others if there is a conflict.
By default the system configuration and local configuration are empty. Global configuration is where the changes are made and this is the file whose location I want to change.
To move the default location of the global configuration file we need to edit another of the Git initialisation files. This one is called profile and on a Windows machine it lives here:
C:\Program Files\Git\etc\profile
The directory in the middle really is called etc, I’m not abbreviating anything. Navigate to the file and edit it. It is a reasonably large file (Code 3.1).
# 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 ... # 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
. . .
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="D:\2500 Git Projects"
Code 3.1 Git profile configuration file additions |
To change the default (home) directory we need to add a line to the very end of the file:
HOME="D:\2500 Git Projects"
The syntax for this is:
HOME=path\to\home\folder
If any of the directory names contain a space, put double quotes around the whole thing as I did with:
HOME="D:\2500 Git Projects"
Now we just need to move the existing .gitconfig (the one that was created when Git Bash was installed) to the new location.
In Windows Explorer navigate to:
C:\Users\<username>\
Where <username>; is your user name on the machine. This is what mine looks like:
Select .gitconfig and then the file ( ), navigate to the new home directory—in my case this is:
D:\2500 Git Projects
And
the cut file there ( ).That’s it.
This is much easier. On the desktop there will be Git Bash icon:
Right click this icon and select Figure 3.22):
, this opens the shortcut dialogue box (Two things to change here:
First in the
box, enter the path to the new home directory. In my case this is:"D:\2500 Git Projects"
Next, in the --cd-to-home entry at the end. The final thing should look like Figure 3.23:
box remove theThat’s it, click
and then click the icon to start Git Bash. This time it will start in your new home directory. Mine looks like this:The bit in yellow is the path to the current directory (it’s in the UNIX style with D: replaced with /d and obliques (/) in place of the reverse obliques (\) that Windows uses for path names—but you get the idea).