默认的Git配置文件应该包含什么?

8

通过git config可以设置众多选项,其中文档化的选项已经让人眼花缭乱。在这些选项中,有哪些是每个开发者都应该设置的(比如user.email)?在常见情况下应设置哪些最常见的选项(例如在Windows上设置core.autocrlf=input)?但请避免宗教争论(例如对于core.whitespace唯一可接受的设置是tab-in-indent)。

2个回答

9
您的全局git配置文件(~/.gitconfig)应该包含适用于所有仓库的设置。主要是像user.nameuser.emailcore.editormergediff这样的设置应该保持一致。话虽如此,我也喜欢启用colorcore.pagerrerererebase.autosquash以及一系列别名。
[color]
    filemode = false
    diff = auto
    status = auto
    branch = auto
    pager = true
[alias]
    b = branch
    ci = commit
    co = checkout
    cob = checkout -b
    d = diff
    l = log
    lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
    lga = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative --branches
    st = status
    fixup = !sh -c 'git commit -a -m \"fixup! $(git log -1 --format='%s' $@)\"' -
    squash = !sh -c 'git commit -a -m \"squash! $(git log -1 --format='%s' $@)\"' -
    ri = rebase --interactive
    rc = rebase --continue
    pr = push gerrit HEAD:refs/for/master
    mt = mergetool
[user]
    email = REDACTED
    name = Matt Henkel
[core]
    pager = less -FRSX
    excludes = ~/.gitexcludes
    editor = vim
[rerere]
    enabled = true
    autoupdate = true
[rebase]
    autosquash = true
[merge]
    tool = kdiff3
[mergetool "kdiff3"]
    keepBackup = false
    trustExitCode = false
[diff]
    tool = kdiff3

14
你认为你可以解释和证明每一项,而不是像“你给我看你的,我再给你看我的”那样吗?这似乎是一个很长且充满个性化内容的清单。哪些应该被放入“every”配置中? - shemnon
每个人(everybody)通常只是指:user.name(用户名)、user.email(用户邮箱)、core.editor(Git编辑器)、merge(合并)、和diff(差异)。我只是包含了我的共享全局配置以供参考。 - Guildencrantz

5

以下是一些最常见的配置设置的注释列表。当然,每个人的环境/语言/操作系统/git工作流程都不同,所以您可能需要对其进行一些调整,但这些是一些最常见的配置变量。

[user]
    # these are about the most basic and should pretty much always exist
    email = your-email@example.com
    name = Your Name
[core]
    # if you use windows
    #autocrlf = true

    # use aggressive compression
    # can make your repo smaller but can also be slow
    compression = 9

    # lets you define a global .gitignore for all those 
    # *.swp, *~, *.o, etc things that you're frequently 
    # sticking in .gitignore
    excludesfile = ~/.gitignore_global

    # tells git to ignore file permission changes
    filemode = false

    # lets you tweak the default pager
    # see `man less` for the meaning of these flags
    pager = 'less -FRSX'

    # probably not a good default for most projects, 
    # but you should uncomment with something based on your needs
    #whitespace = tab-in-indent

[color]
    # this turns on default colors for many commands
    # or you can customize specific colors per command (see [3] for example)
    ui = auto

[rerere]
    # google `git rerere`, basically git remembers your
    # partial merge choices and replays them next time
    enabled = true
    autoupdate = true

[push]
    # lets you say just `git push origin` to push the current branch
    default = current 

[alias]
    # this is the most subjective section 

    # aliases are useful if you either frequently typo certain words
    # or else if you are used to another VC like cvs or svn
    co = checkout
    ci = commit
    st = status
    br = branch -av
    brdel = branch -D

    # Show all of my configured aliases
    aliases = !git config --list | grep 'alias\\.' | sed 's/alias\\.\\([^=]*\\)=\\(.*\\)/\\1\\ \t => \\2/' | sort

    # pretty much everybody has their favorite log format view
    # you can find dozens of variations with a quick google
    # here are couple of the most common (the first is my favorite)
    lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
    hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short

汇总自多个来源:

  1. https://githowto.com/aliases
  2. https://www.javacodegeeks.com/2013/06/git-configuration-options-you-cant-miss.html
  3. http://michaelwales.com/articles/make-gitconfig-work-for-you/#colored-output
  4. https://wildlyinaccurate.com/useful-git-configuration-items/

说明:这些来源提供了关于 Git 配置、别名和颜色配置等相关信息。

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