Changing .gitconfig location on Windows

WindowsGit

Windows Problem Overview


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?

Windows Solutions


Solution 1 - Windows

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).

Solution 2 - Windows

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

Solution 3 - Windows

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.

Solution 4 - Windows

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

Solution 5 - Windows

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.

Solution 6 - Windows

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.

Solution 7 - Windows

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:

Solution 8 - Windows

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

Solution 9 - Windows

  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.

Solution 10 - Windows

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\.

Solution 11 - Windows

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
}

Solution 12 - Windows

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.

Solution 13 - Windows

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

Solution 14 - Windows

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.

Solution 15 - Windows

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRookView Question on Stackoverflow
Solution 1 - WindowsMartin v. LöwisView Answer on Stackoverflow
Solution 2 - WindowsMicTechView Answer on Stackoverflow
Solution 3 - WindowsJoe BrinkmanView Answer on Stackoverflow
Solution 4 - WindowsStefan NäweView Answer on Stackoverflow
Solution 5 - WindowsscottsandersdevView Answer on Stackoverflow
Solution 6 - WindowsBrian BlackburnView Answer on Stackoverflow
Solution 7 - WindowsAlitiView Answer on Stackoverflow
Solution 8 - WindowsMicah ZoltuView Answer on Stackoverflow
Solution 9 - WindowscuasijoeView Answer on Stackoverflow
Solution 10 - WindowsFatAlbertView Answer on Stackoverflow
Solution 11 - WindowsAde MillerView Answer on Stackoverflow
Solution 12 - WindowsehillerView Answer on Stackoverflow
Solution 13 - WindowsRalph SchwerdtView Answer on Stackoverflow
Solution 14 - WindowsKrishnakanth AllikaView Answer on Stackoverflow
Solution 15 - WindowsAmirView Answer on Stackoverflow