Rook
Rook

Reputation: 62528

Changing .gitconfig location on Windows

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?

Upvotes: 142

Views: 218507

Answers (17)

Chris F Carroll
Chris F Carroll

Reputation: 12360

A visual guide for Windows users to set a GIT_CONFIG_GLOBAL environment variable for all processes including GUI IDEs.

Exit and restart any running IDEs or command lines to make this take effect immediately.

GUI Guide for Windows users to set GIT_CONFIG_GLOBAL environment variable on user login

Upvotes: 0

Rephila
Rephila

Reputation: 19

The default .gitconfig file is typically located in your home directory (~ in Unix-like systems, or C:\Users\YourUsername in Windows). Git expects to find its configuration file in this location by default, and there isn't a built-in option to change this default behavior.

However, you can override the default location of the .gitconfig file by setting the GIT_CONFIG environment variable. By setting this variable, you can instruct Git to look for its configuration file in a different location.

For example, if you want to use a .gitconfig file located in a directory named custom-config within your home directory, you can set the GIT_CONFIG variable like this:

export GIT_CONFIG="$HOME/custom-config/.gitconfig"

Or, on Windows:

setx GIT_CONFIG "%USERPROFILE%\custom-config\.gitconfig"

After setting this environment variable, Git will look for the .gitconfig file in the specified location instead of the default location in your home directory.

Keep in mind that setting GIT_CONFIG will override the default behavior for all Git commands and sessions in your environment.

Upvotes: 1

Micah Zoltu
Micah Zoltu

Reputation: 7422

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.

Upvotes: 35

Amir
Amir

Reputation: 1834

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

Upvotes: -1

Krishnakanth Allika
Krishnakanth Allika

Reputation: 790

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.

Upvotes: 1

Ralph Schwerdt
Ralph Schwerdt

Reputation: 19

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

Upvotes: 0

cuasiJoe
cuasiJoe

Reputation: 1149

  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.

Upvotes: 5

scottsandersdev
scottsandersdev

Reputation: 161

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.

Upvotes: 11

Martin v. L&#246;wis
Martin v. L&#246;wis

Reputation: 127447

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

Upvotes: 85

ehiller
ehiller

Reputation: 1547

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.

Upvotes: 4

Aliti
Aliti

Reputation: 2095

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:

Upvotes: 6

Joe Brinkman
Joe Brinkman

Reputation: 1892

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.

Upvotes: 50

Brian Blackburn
Brian Blackburn

Reputation: 111

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.

Upvotes: 8

Ade Miller
Ade Miller

Reputation: 13723

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
}

Upvotes: 3

FatAlbert
FatAlbert

Reputation: 4970

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

Upvotes: 3

MicTech
MicTech

Reputation: 45003

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

Upvotes: 56

Stefan N&#228;we
Stefan N&#228;we

Reputation: 3104

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

Upvotes: 12

Related Questions