Changing .gitconfig location on Windows

后端 未结 13 2357
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 11:49

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

相关标签:
13条回答
  • 2020-11-27 11:51

    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
    }
    
    0 讨论(0)
  • 2020-11-27 11:53

    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.

    0 讨论(0)
  • 2020-11-27 11:53

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

    0 讨论(0)
  • 2020-11-27 11:56

    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:
    
    0 讨论(0)
  • 2020-11-27 11:57

    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.

    0 讨论(0)
  • 2020-11-27 12:01

    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.

    0 讨论(0)
提交回复
热议问题