代码之家  ›  专栏  ›  技术社区  ›  Rook

在Windows上更改.gitconfig位置

git
  •  92
  • Rook  · 技术社区  · 14 年前

    默认情况下,在Windows Git上,将global.gitconfig放入 c:\documents and settings\user\

    我如何更改该位置以便.gitconfig存储在 c:\my_configuration_files\ ?

    能做到吗?

    10 回复  |  直到 5 年前
        1
  •  63
  •   ChrisWue Nikesh K    7 年前

    如果你设置 HOME c:\my_configuration_files\ ,然后git将在那里找到.gitconfig。描述了编辑环境变量 here . 您需要设置HOME变量,然后重新打开任何cmd.exe窗口。使用“set”命令验证HOME是否确实指向正确的值。

    --global 所有git调用的标志(加上忽略任何本地.git/config文件)。

        2
  •  43
  •   alex    12 年前

    为此更改主目录是错误的。最好是为gitconfig创建到主目录的符号链接。

    1. 将.gitconfig从用户主目录移动到所需的目录。
    2. 以管理员身份运行命令行
    3. 输入mklink.gitconfig\PathForNewLocationOfConfig.gitconfig
        3
  •  29
  •   Joe Brinkman    9 年前

    我用一种与其他配置文件稍有不同的方法解决了这个问题。 Git Config 包括 它允许您指向另一个位置的配置文件。然后,该备用位置将被导入并展开,就像它是.gitconfig文件的一部分一样。所以现在我在.gitconfig中只有一个条目:

    [include]
       path = c:\\path\\to\\my.config
    

        4
  •  12
  •   Stefan Näwe    14 年前

    看看里面 环境 git help config .

        5
  •  11
  •   smonff David Verdin    6 年前

    我不是Git高手,但从搜索对我来说最简单的解决方案开始 C:\Program Files (x86)\Git\etc profile 在文本编辑器中。

    有一个 if # Set up USER's home directory . 我拿出了 如果 语句并将其放入我希望gitconfig位于的本地目录中,然后我将现有的gitconfig文件(位于网络驱动器上)复制到该位置。

        6
  •  6
  •   Brian Blackburn    9 年前

    我通过制作一个批处理脚本解决了这个问题,该脚本将只覆盖该应用程序的HOMEDRIVE和HOMEPATH env变量。然后更改了“开始”菜单快捷方式,改为指向该批处理脚本。

        7
  •  3
  •   FatAlbert    12 年前

    如果您在windows上,并且在更改环境变量或 mklink

    右键单击Git Bash.exe,单击properties并将“Start in”属性更改为 c:\my_configuration_files\ .

        8
  •  3
  •   Ade Miller    12 年前

    我也想做同样的事。我能找到的最好的办法就是“MicTech的解决方案”。但是,正如@MotoWilliams所指出的,Git对.gitconfig文件所做的任何更新都无法保存,该文件将链接替换为只包含新设置的新文件。

    我将global.gitconfig文件与许多其他全局脚本和工具一起保存在repo中。我要做的就是记住签入脚本附加到全局文件中的任何更改。

    9月9日: 更新以检测添加到配置文件的新条目是否重复并忽略它们。这对于SourceTree之类的工具非常有用,如果它们找不到现有的更新并且不遵循includes,SourceTree将编写新的更新。

    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
    }
    
        9
  •  3
  •   Aliti    8 年前

    首先检查HOME设置,然后将HOME和HOMEDRIVE更改为现有目录。

    c:\git>set HOME
    HOME=U:\
    HOMEDRIVE=U:
    HOMEPATH=\
    

    然后换车回家

    set HOME=c:\tmp
    set HOMEDRIVE=C:
    
        10
  •  2
  •   ehiller azamsharp    7 年前

    $XDG_CONFIG_HOME/git/config -第二个特定于用户的配置文件。如果 $XDG_CONFIG_HOME

    只是最近才加的。这是2.15.0版的。

    对我有用。

        11
  •  1
  •   cuasijoe    6 年前
    1. 更改为文件夹 %PROGRAMFILES%\Git\etc
    2. profile
    3. 添加到第一行 HOME="c:\location_were_you_want_gitconfig"
    4. 多恩

    文件权限通常受到限制,因此相应地更改它们,否则您将无法保存更改。

        12
  •  0
  •   Ralph Schwerdt    5 年前

    1. 将“.gitconfig”从旧目录复制到%USERPROFILE%(c:\users\username中的标准)

    screen shot before

    screen shot after