Git:重置git配置文件的最简单方法

44

我终于开始学习Git了。然而,我之前不明智地通过Sourcetree开始使用它(在我真正知道该怎么做之前)。现在我正在尝试进行操作,但是我有一个相当长的设置列表。当我输入:

git config --list

我得到了以下结果:

core.excludesfile=~/.gitignore
core.legacyheaders=false
core.quotepath=false
core.pager=less -r
mergetool.keepbackup=true
push.default=simple
color.ui=auto
color.interactive=auto
repack.usedeltabaseoffset=true
alias.s=status
alias.a=!git add . && git status
alias.au=!git add -u . && git status
alias.aa=!git add . && git add -u . && git status
alias.c=commit
alias.cm=commit -m
alias.ca=commit --amend
alias.ac=!git add . && git commit
alias.acm=!git add . && git commit -m
alias.l=log --graph --all --pretty=format:'%C(yellow)%h%C(cyan)%d%Creset %s %C(white)- %an, %ar%Creset'
alias.ll=log --stat --abbrev-commit
alias.lg=log --color --graph --pretty=format:'%C(bold white)%h%Creset -%C(bold green)%d%Creset %s %C(bold green)(%cr)%Creset %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
alias.llg=log --color --graph --pretty=format:'%C(bold white)%H %d%Creset%n%s%n%+b%C(bold blue)%an <%ae>%Creset %C(bold green)%cr (%ci)' --abbrev-commit
alias.d=diff
alias.master=checkout master
alias.spull=svn rebase
alias.spush=svn dcommit
alias.alias=!git config --list | grep 'alias\.' | sed 's/alias\.\([^=]*\)=\(.*\)/\1\     => \2/' | sort
include.path=~/.gitcinclude
include.path=.githubconfig
include.path=.gitcredential
diff.exif.textconv=exif
credential.helper=osxkeychain
core.excludesfile=/Users/myusername/.gitignore_global
difftool.sourcetree.cmd=opendiff "$LOCAL" "$REMOTE"
difftool.sourcetree.path=
mergetool.sourcetree.cmd=/Applications/SourceTree.app/Contents/Resources/opendiff-w.sh "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"
mergetool.sourcetree.trustexitcode=true
user.name=My Name
user.email=myemail@email.com

因为我正在学习的教程并没有展示所有这些内容,所以这似乎比我想要开始的多。

有没有一种方法可以将配置文件“重置”为默认状态?


25
只需执行 rm ~/.gitconfig,然后重新开始即可吗? - choroba
好像已经摆脱了那些额外的参数。谢谢! - bboysupaman
5
我终于开始学习 Git 了。- 这是我们大多数人的情况 :) - Number945
可能是重置git设置的重复问题。 - leonheess
2个回答

56
请注意,git config --list 显示了以下内容:
  • 系统设置 (<path/to/git/config>),即 Git 安装的位置。
    (在 Windows 上,默认为 C:\Program Files\Git
  • 全局设置 ($HOME/.gitconfig)。
    在 Windows 上,$HOME 应为 %USERPROFILE%
  • 本地设置 (path/to/repo/.git/config)。
要查看您添加到存储库的设置,请执行以下操作:
git config --local --list

删除多个值:

git config [<file-option>] --unset-all name [value_regex]
git config [<file-option>] --remove-section name

例如:git config --local --remove-section alias 可以摆脱别名。
这比仅仅摆脱.git/config文件更加精确。
通常情况下,因为代理设置可能已经添加到全局的所有存储库中,所以git config --local --list不会显示我的代理设置。
 git config --global --list

这将在~/.gitconfig%USERPROFILE%\.gitconfig中设置。
注意:如果您想删除特定值(而不是正则表达式),请使用Git 2.30(2021年第一季度)。
git configman的各种子命令都可以使用“--literal-value”选项将value_regex选项作为字面字符串。

查看 提交记录 c902618 (2020年11月25日) 由 Junio C Hamano (gitster) 提交。
查看 提交记录 3f1bae1, 提交记录 c90702a, 提交记录 fda4394, 提交记录 d156719, 提交记录 2076dba, 提交记录 247e2f8, 提交记录 504ee12 (2020年11月25日) 由 Derrick Stolee (derrickstolee) 提交。
(合并于 提交记录 a10e784,2020年12月8日,由 Junio C Hamano -- gitster -- 完成)

config: 添加--fixed-value选项,未实现

Signed-off-by: Derrick Stolee

'git config'(man)内置命令有多个操作需要使用'value-pattern'参数。这会在期望精确匹配而不是正则表达式匹配时导致混淆,特别是当输入字符串包含元字符时。虽然调用者可以自己转义模式,但更友好的方式是允许一个参数禁用模式匹配,以支持精确字符串匹配。
添加一个新的'--fixed-value'选项,目前不改变行为。实现将由后续更改针对每个适当的操作进行填充。现在,请检查和测试--fixed-value是否会在与不兼容的操作或没有'value-pattern'参数的情况下中止命令。
之所以选择“--fixed-value”而不是像“--fixed”这样的简单名称,是因为某些命令除了值之外还允许在键上使用正则表达式。

git config现在在其手册页面中包含以下内容:

--fixed-value

当与value-pattern参数一起使用时,将value-pattern视为精确字符串而不是正则表达式。这将限制匹配的名称/值对仅为那些值完全等于value-pattern的情况。


1
我们可以在 ~/.gitconfig 上编辑全局设置。我想要通过 git config --global user.name 或 user.email 来更改 Git 的全局配置。简单来说,使用 nano 编辑 nano ~/.gitconfig 并保存即可。参考链接 - Abdallah Okasha
1
@AbdallahOkasha 确实如此。我更喜欢使用 git config 命令,以避免在直接编辑配置文件时发生任何意外。 - VonC
真正的糟糕答案。$HOME是什么?它在C:\Program Files\Git中吗?path/to/git/config在哪里?为什么git config --local --list不显示我的代理设置?如果我知道答案,你的回答会帮助我。哈哈 - Amir
1
@Amir,我已经编辑了我的答案,以澄清在Windows上"$HOME"是什么。 - VonC
@Amir,我已经编辑了答案来回答你的另一个问题:“为什么git config --local --list不显示我的代理设置?” - VonC
有人可以帮我解决这个问题吗?https://stackoverflow.com/questions/61830920/where-to-find-the-config-directory-of-git-on-mac-os - Ken Verganio

6

移除所有自定义设置:

rm ~/.gitconfig

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接