Changing .gitconfig location on Windows

后端 未结 13 2464
伪装坚强ぢ
伪装坚强ぢ 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
    }
    

提交回复
热议问题