131

By default on Windows Git places global .gitconfig in c:\documents and settings\user\

How can I change that position so .gitconfig is stored in c:\my_configuration_files\?

Can it be done at all?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Rook
  • 60,248
  • 49
  • 165
  • 242
  • Just wanted to add I am running into this too. I have all my other global git config stuff ("exludesfiles" and "attrirutesfile") in %userprofile%/.git/.gitignore (so in a folder), but the .gitconfig I would love for it to sit right next to them, but has to be on the root of %userprofile%.. just really annoying. – WORMSS Oct 31 '14 at 15:38
  • 8
    Any progress since the question has been asked? My .gitconfig sits on a network drive by default, meaning that it can't read it when I'm offline. – Lech Rzedzicki Jun 24 '15 at 06:06
  • The real question behind this is: why would you want to do this? – Johan Boulé Oct 21 '21 at 11:15

15 Answers15

84

If you set HOME to c:\my_configuration_files\, then git will locate .gitconfig there. Editing environment variables is described here. You need to set the HOME variable, then re-open any cmd.exe window. Use the "set" command to verify that HOME indeed points to the right value.

Changing HOME will, of course, also affect other applications. However, from reading git's source code, that appears to be the only way to change the location of these files without the need to adjust the command line. You should also consider Stefan's response: you can set the GIT_CONFIG variable. However, to give it the effect you desire, you need to pass the --global flag to all git invocations (plus any local .git/config files are ignored).

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
  • 4
    Hmm, not sure if I understood correctly but won't changing HOME have an influence to also all other applications which use it? Isn't there a way to change HOME, but local to Git? – Rook Oct 30 '10 at 03:39
  • Yes, it would have effect on other applications as well. You asked how to change the location, and I told you. See my edit. – Martin v. Löwis Oct 30 '10 at 07:46
  • 21
    Has anyone actually got this to work without the side effects of moving HOME messing up a whole load of other things? – Ade Miller Sep 15 '13 at 06:36
  • This most likely is a solution that works on Windows platform. It worked also for me. I used Chocolatey's refreshenv script to refresh the environment variable HOME that I set (user environment variable). After that, I got the desired file location. – Tore Aurstad Apr 15 '19 at 20:49
  • 1
    I had to reinstall Git for this to work. If I just added a new environment variable named HOME and moved the `.gitconfig` file to that directory, it didn't work. – Csa77 Aug 20 '19 at 08:48
  • For those who can't change HOME, this is the answer in 2021: https://stackoverflow.com/a/70345376/732673 – Josh Noe Jan 11 '23 at 20:57
54

Change HOME directory for this is wrong. Better is create symbolic link for gitconfig to HOME directory.

  1. Move your .gitconfig from user home directory, to the directory where you want.
  2. Run command line as Administrator
  3. Go to your user home directory
  4. Enter mklink .gitconfig \PathForNewLocationOfConfig.gitconfig
alex
  • 479,566
  • 201
  • 878
  • 984
MicTech
  • 42,457
  • 14
  • 62
  • 79
  • 11
    It might worth adding that if you run git config --global to add an item to your config that your symlink will be overwritten with actual copy of the .gitconfig that is being used. I ran into this issue when testing a location where \PathForNewLocationOfConfig.gitconfig was my Dropbox folder – MotoWilliams Feb 06 '12 at 20:10
  • @MotoWilliams very annoying indeed. Are there any workarounds I wonder? Other than switching to Linux :) – Halil Özgür May 15 '12 at 08:40
  • 10
    If your .gitconfig is located on a network drive you may get a "The device does not support symbolic links" message which rules this option out. – noonand May 05 '15 at 15:07
  • 7
    I don't think this solution will work for anyone. In our company, `$HOME` is on a network drive and whenever there's any problem with the network connection, also the link is unreachable. – andreee Dec 19 '16 at 10:52
  • 2
    @MotoWilliams revisiting this in 2017: the symlink stays intact after editing with --global – Anemoia Jul 18 '17 at 20:11
  • 6
    Here, group police (AD) sets HOMEDRIVE, HOMEPATH and HOMESHARE, but not plain,old, simple HOME. In the absence of HOME, git seems to use some combination of the previous three. But setting HOME (via the usual methodology for setting user-specific environment variables) works: GIT uses the HOME value you set, and grouppolicy does not clobber HOME. – David I. McIntosh Sep 15 '17 at 21:07
45

I have solved this problem using a slightly different approach that I have seen work for other configuration files. Git Config supports includes that allows you to point to a configuration file in another location. That alternate location is then imported and expanded in place as if it was part of .gitconfig file. So now I just have a single entry in .gitconfig:

[include]
   path = c:\\path\\to\\my.config

Any updates written by Git to the .gitconfig file will not overwrite my include path. It does mean that occasionally I may need to move values from .gitconfig to my.config.

Joe Brinkman
  • 1,842
  • 2
  • 16
  • 20
  • +1: no odd configurations and my global gitconfig is something I store in a git repo itself, so keeping track of what's in there is my job. When making global changes I just edit the global file by hand. So this works perfectly for me, and requires no oddity like changing your "HOME" environment variable, which is a strange idea just for the sake of getting Git's config where you want it. The home directory after all has other meanings to many other programs. Also you can put a different identity at the top and keep your key settings the same across identities with this solution. Excellent! – user62177541 Aug 20 '16 at 03:29
  • 2
    I wonder if it is possible to have git config write to the file in the [include]d file instead. – Jonathan Nazario Dec 09 '17 at 08:23
  • 1
    @JonathanNazario `git config --file "included.file"` – Pero P. Oct 11 '19 at 23:23
  • Making a bash alias seems to work quite well: `alias git="git -c include.path $PATH_TO_GITCONFIG"` Then, you don't need to have any `$HOME/.gitconfig` at all. – Kevin Dec 14 '21 at 14:42
16

As of at least 2.34.1, you can now set GIT_CONFIG_GLOBAL environment variable to a path to your .gitconfig file. See https://git-scm.com/docs/git-config#ENVIRONMENT for more details.

Brandlingo
  • 2,817
  • 1
  • 22
  • 34
Micah Zoltu
  • 6,764
  • 5
  • 44
  • 72
  • 2
    For those struggeling with centrally set values for HOMEDRIVE and HOMEPATH the above solution is nice. If you want to ensure the global .gitconf is put into c:\users\username\ use the following PS-script: `[System.Environment]::SetEnvironmentVariable('GIT_CONFIG_GLOBAL',$env:USERPROFILE + '\.gitconfig','User')` – Jan Mar 25 '22 at 12:46
  • 1
    This is clean and elegant. No need to run Git with special batch scripts or command line switches, or modify the Git installation in any way. Doesn't interfere with any other software which relies on $HOME. – Hydrargyrum Jan 23 '23 at 03:02
13

Look in the FILES and ENVIRONMENT section of git help config.

Stefan Näwe
  • 3,040
  • 1
  • 18
  • 19
11

I'm no Git master, but from searching around the solution that worked easiest for me was to just go to C:\Program Files (x86)\Git\etc and open profile in a text editor.

There's an if statement on line 37 # Set up USER's home directory. I took out the if statement and put in the local directory that I wanted the gitconfig to be, then I just copied my existing gitconfig file (was on a network drive) to that location.

smonff
  • 3,399
  • 3
  • 36
  • 46
scottsandersdev
  • 161
  • 1
  • 8
  • Thanks a ton. While all other options failed for me, this just saved me a million git statements multiplied by 6 seconds :D here is what I did... ` HOME="$USERPROFILE\Downloads\Tools\Git_Config"` – PravyNandas Oct 06 '17 at 11:44
  • 3
    The word 'home' (or anything looking similiar to you line :37) no longer exists in git 2.24 (December 2019). – Frank N Dec 02 '19 at 08:33
8

For me, changing the Start In location (of git-gui at least) did not affect where it looked for .gitconfig. My setup at work mounts U: for our home, but we do not have permission to write in U: directly, only subdirectories which were created for us inside, so this was a deal-breaker for me.

I solved the problem by making a batch script which would override the HOMEDRIVE and HOMEPATH env variables just for that application. Then changed my Start menu shortcut to point to that batch script instead.

Brian Blackburn
  • 111
  • 1
  • 3
6

First check HOME setting, then change HOME and HOMEDRIVE to a existing dir.

c:\git>set HOME
HOME=U:\
HOMEDRIVE=U:
HOMEPATH=\

then change HOME and HOMEDRIVE by

set HOME=c:\tmp
set HOMEDRIVE=C:
Aliti
  • 2,025
  • 2
  • 27
  • 39
4
  1. Change to folder %PROGRAMFILES%\Git\etc
  2. Edit file profile
  3. Add to first line HOME="c:\location_were_you_want_gitconfig"
  4. Done

Note: The file permissions are usually restricted, so change them accordingly or you won't be able to save your changes.

cuasiJoe
  • 1,089
  • 10
  • 12
  • This is not working as of git 2.24, Win10, December 2019. `git config --list --show-origin` shows, the original HOME is still in place. – Frank N Dec 02 '19 at 08:52
3

If you are on windows and having problem either changing environment variables or mklink because of insufficient privileges, an easy solution to your problem is to start git batch in another location.

Just right click on Git Bash.exe, click properties and change the "Start in" property to c:\my_configuration_files\.

FatAlbert
  • 4,890
  • 6
  • 22
  • 34
  • Wow this totally work with ease. Created a new short cut and under 'properties->start in' changed `%HOMEDRIVE%%HOMEPATH%` to `c:\my_repo_folder\`. Worked a charm. – Shane Jun 12 '15 at 16:14
  • 3
    This didn't work for me. The .gitconfig file was still loaded from my home directory – Dave B May 17 '17 at 17:01
3

I wanted to do the same thing. The best I could find was @MicTech's solution. However, as pointed out by @MotoWilliams this does not survive any updates made by Git to the .gitconfig file which replaces the link with a new file containing only the new settings.

I solved this by writing the following PowerShell script and running it in my profile startup script. Each time it is run it copies any settings that have been added to the user's .gitconfig to the global one and then replaces all the text in the .gitconfig file with and [include] header that imports the global file.

I keep the global .gitconfig file in a repo along with a lot of other global scripts and tools. All I have to do is remember to check in any changes that the script appends to my global file.

This seems to work pretty transparently for me. Hope it helps!

Sept 9th: Updated to detect when new entries added to the config file are duplicates and ignore them. This is useful for tools like SourceTree which will write new updates if they cannot find existing ones and do not follow includes.

function git-config-update
{
  $localPath = "$env:USERPROFILE\.gitconfig".replace('\', "\\")
  $globalPath = "C:\src\github\Global\Git\gitconfig".replace('\', "\\")

  $redirectAutoText = "# Generated file. Do not edit!`n[include]`n  path = $globalPath`n`n"
  $localText = get-content $localPath

  $diffs = (compare-object -ref $redirectAutoText.split("`n") -diff ($localText) | 
    measure-object).count

  if ($diffs -eq 0)
  {
    write-output ".gitconfig unchanged."
    return
  }

  $skipLines = 0
  $diffs = (compare-object -ref ($redirectAutoText.split("`n") | 
     select -f 3) -diff ($localText | select -f 3) | measure-object).count
  if ($diffs -eq 0)
  {
    $skipLines = 4
    write-warning "New settings appended to $localPath...`n "
  }
  else
  {
    write-warning "New settings found in $localPath...`n "
  }
  $localLines = (get-content $localPath | select -Skip $skipLines) -join "`n"
  $newSettings = $localLines.Split(@("["), [StringSplitOptions]::RemoveEmptyEntries) | 
    where { ![String]::IsNullOrWhiteSpace($_) } | %{ "[$_".TrimEnd() }

  $globalLines = (get-content  $globalPath) -join "`n"
  $globalSettings =  $globalLines.Split(@("["), [StringSplitOptions]::RemoveEmptyEntries)| 
    where { ![String]::IsNullOrWhiteSpace($_) } | %{ "[$_".TrimEnd() }

  $appendSettings = ($newSettings | %{ $_.Trim() } | 
    where { !($globalSettings -contains $_.Trim()) })
  if ([string]::IsNullOrWhitespace($appendSettings))
  {
    write-output "No new settings found."
  }
  else
  {
    echo $appendSettings
    add-content $globalPath ("`n# Additional settings added from $env:COMPUTERNAME on " + (Get-Date -displayhint date) + "`n" + $appendSettings)
  }
  set-content $localPath $redirectAutoText -force
}
Ade Miller
  • 13,575
  • 1
  • 42
  • 75
3

As someone who has been interested in this for a VERY LONG TIME. See from the manual:

$XDG_CONFIG_HOME/git/config - Second user-specific configuration file. If $XDG_CONFIG_HOME is not set or empty, $HOME/.config/git/config will be used. Any single-valued variable set in this file will be overwritten by whatever is in ~/.gitconfig. It is a good idea not to create this file if you sometimes use older versions of Git, as support for this file was added fairly recently.

Which was only recently added. This dump is from 2.15.0.

Works for me.

ehiller
  • 1,346
  • 17
  • 32
1

Solution without having to change Windows HOME variable
OS: Windows 10
git version: 2.27.0.windows.1

I use portable version of Git, so all my config files are on my pen-drive(E:). This is what worked for me:

  1. Download portable Git from https://git-scm.com/. Run the file and install it in the required location. I installed it in E:\git.
  2. Run git-bash.exe from E:\git.
  3. I wanted to put .gitconfig and other bash files in E, so I created a folder called home where I want them all in.
    mkdir home
  4. Go to etc folder and open the file called profile (In my case, it's E:\git\etc\profile)
  5. Add absolute path to the home directory we created (or to the directory where you want to have your .gitconfig file located) at the end of the profile file.
    HOME="E:\git\home"

Now it no longer searches in the C:\Users<username> directory for .gitconfig but only looks in your set path above.

$ git config --global --list
fatal: unable to read config file 'E:/git/home/.gitconfig': No such file or directory

It gave an error because there isn't a .gitconfig file there yet. This is just to demonstrate that we have successfully changed the location of the .gitconfig file without changing the HOME directory in Windows.

0

For me it worked very simple:

  1. Copy ".gitconfig" from old directory: to %USERPROFILE% (standard in "c:\users\username")
  2. Right click on start-icons of GITGUI and GITBASH and change "run in": "%HOMEDRIVE%%HOMEPATH%" to "%USERPROFILE%". Of cource you can use any other directory instead of "%USERPROFILE%".

screen shot before

screen shot after

-1

1- open D:\PortableApps\Git-2.31.1PortableByAmir\etc\profile bye notepad

2- add this to profile file:

HOME="/PortableHome"

3- create PortableHome folder in D:\PortableApps\Git-2.31.1PortableByAmir\

4- copy

C:\Users\Amir\.gitconfig
C:\Users\Amir\.git-credentials

to

D:\PortableApps\Git-2.31.1PortableByAmir\PortableHome

Note: D:\PortableApps\Git-2.31.1PortableByAmir\bin\bash.exe is not a portable git only open D:\PortableApps\Git-2.31.1PortableByAmir\git-bash.exe

Amir
  • 1,638
  • 19
  • 26