在 Windows 上更改 .gitconfig 位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4050905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:41:50  来源:igfitidea点击:

Changing .gitconfig location on Windows

windowsgit

提问by Rook

By default on Windows Git places global .gitconfig in c:\documents and settings\user\

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

How can I change that position so .gitconfig is stored in c:\my_configuration_files\?

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

Can it be done at all?

完全可以做到吗?

采纳答案by Martin v. L?wis

If you set HOMEto 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.

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

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 --globalflag to all git invocations (plus any local .git/config files are ignored).

更改HOME当然也会影响其他应用程序。但是,从阅读 git 的源代码来看,这似乎是无需调整命令行即可更改这些文件位置的唯一方法。您还应该考虑 Stefan 的回应:您可以设置 GIT_CONFIG 变量。然而,为了给它你想要的效果,你需要将--global标志传递给所有 git 调用(加上任何本地 .git/config 文件都被忽略)。

回答by MicTech

Change HOME directory for this is wrong. Better is create symbolic link for gitconfig to HOME directory.

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

  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
  1. 将您的 .gitconfig 从用户主目录移动到您想要的目录。
  2. 以管理员身份运行命令行
  3. 转到您的用户主目录
  4. 输入 mklink .gitconfig \PathForNewLocationOfConfig.gitconfig

回答by Joe Brinkman

I have solved this problem using a slightly different approach that I have seen work for other configuration files. Git Configsupports includesthat 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:

我已经使用一种稍微不同的方法解决了这个问题,我已经看到这种方法适用于其他配置文件。 Git Config支持包含允许您指向另一个位置的配置文件。然后该备用位置被导入并扩展到位,就好像它是 .gitconfig 文件的一部分。所以现在我在 .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.

Git 写入 .gitconfig 文件的任何更新都不会覆盖我的包含路径。这确实意味着有时我可能需要将值从 .gitconfig 移动到 my.config。

回答by Stefan N?we

Look in the FILESand ENVIRONMENTsection of git help config.

查看 的文件环境部分git help config

回答by scottsandersdev

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\etcand open profilein a text editor.

我不是 Git 高手,但是通过搜索对我来说最简单的解决方案,只需转到文本编辑器中C:\Program Files (x86)\Git\etc打开即可profile

There's an ifstatement on line 37 # Set up USER's home directory. I took out the ifstatement 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.

if第 37 行有一条声明# Set up USER's home directory。我取出if语句并将其放入我希望 gitconfig 所在的本地目录中,然后我将现有的 gitconfig 文件(位于网络驱动器上)复制到该位置。

回答by Brian Blackburn

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.

对我来说,更改 Start In 位置(至少是 git-gui 的)不会影响它查找 .gitconfig 的位置。我在工作中的设置为我们的家安装了 U:,但我们没有权限直接在 U: 中写入,只有在内部为我们创建的子目录,所以这对我来说是一个交易破坏者。

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.

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

回答by Aliti

First check HOME setting, then change HOME and HOMEDRIVE to a existing dir.

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

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

then change HOME and HOMEDRIVE by

然后通过以下方式更改 HOME 和 HOMEDRIVE

set HOME=c:\tmp
set HOMEDRIVE=C:

回答by FatAlbert

If you are on windows and having problem either changing environment variables or mklinkbecause of insufficient privileges, an easy solution to your problem is to start git batch in another location.

如果您在 Windows 上遇到更改环境变量或mklink权限不足的问题,那么解决问题的一个简单方法是在另一个位置启动 git batch。

Just right click on Git Bash.exe, click properties and change the "Start in" property to c:\my_configuration_files\.

只需右键单击 Git Bash.exe,单击属性并将“开始于”属性更改为c:\my_configuration_files\.

回答by Ade Miller

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.

我想做同样的事情。我能找到的最好的是@MicTech 的解决方案。但是,正如@MotoWilliams 所指出的那样,Git 对 .gitconfig 文件所做的任何更新都不会存在,该文件将链接替换为仅包含新设置的新文件。

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.

我通过编写以下 PowerShell 脚本并在我的配置文件启动脚本中运行它来解决这个问题。每次运行时,它都会将已添加到用户 .gitconfig 的任何设置复制到全局设置,然后将 .gitconfig 文件中的所有文本替换为导入全局文件的 [include] 标头。

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.

我将全局 .gitconfig 文件以及许多其他全局脚本和工具保存在一个 repo 中。我所要做的就是记住检查脚本附加到我的全局文件的任何更改。

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.

9 月 9 日:更新以检测添加到配置文件中的新条目何时重复并忽略它们。这对于像 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
}

回答by ehiller

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

$XDG_CONFIG_HOME/git/config- 第二个特定于用户的配置文件。如果$XDG_CONFIG_HOME未设置或为空,将使用 $HOME/.config/git/config。此文件中设置的任何单值变量都将被 ~/.gitconfig 中的任何内容覆盖。如果您有时使用旧版本的 Git,最好不要创建此文件,因为最近才添加了对该文件的支持。

Which was only recently added. This dump is from 2.15.0.

这是最近才添加的。此转储来自 2.15.0。

Works for me.

对我来说有效。