Reputation: 62528
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
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.
Upvotes: 0
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
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
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
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:
.gitconfig
and other bash files in E, so I created a folder called home where I want them all in.mkdir home
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
Reputation: 19
For me it worked very simple:
Upvotes: 0
Reputation: 1149
%PROGRAMFILES%\Git\etc
profile
HOME="c:\location_were_you_want_gitconfig"
Note: The file permissions are usually restricted, so change them accordingly or you won't be able to save your changes.
Upvotes: 5
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
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
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
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
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
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
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
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
Reputation: 45003
Change HOME directory for this is wrong. Better is create symbolic link for gitconfig to HOME directory.
Upvotes: 56
Reputation: 3104
Look in the FILES and ENVIRONMENT section of git help config
.
Upvotes: 12