Git 配置

1、Mac Git 配置文件

  • 既然已经在系统上安装了 Git,你会想要做几件事来定制你的 Git 环境。每台计算机上只需要配置一次,程序升级时会保留配置信息。你可以在任何时候再次通过运行命令来修改它们。

  • Git 自带一个 git config 的工具来帮助设置控制 Git 外观和行为的配置变量。这些变量存储在三个不同的位置:

    • 1> /etc/gitconfig 文件: 包含系统上每一个用户及他们仓库的通用配置。如果使用带有 –system 选项的 git config 时,它会从此文件读写配置变量。

    • 2> ~/.gitconfig~/.config/git/config 文件:只针对当前用户。可以传递 --global 选项让 Git 读写此文件。

    • 3> 当前使用仓库的 Git 目录中的 config 文件(就是 .git/config):针对该仓库。

  • 每一个级别覆盖上一级别的配置,所以 .git/config 的配置变量会覆盖 /etc/gitconfig 中的配置变量。

2、Windows Git 配置文件

  • 在 Windows 系统中,Git 会查找 $HOME 目录下(一般情况下是 C:\Users\$USER)的 .gitconfi 文件。Git 同样也会寻找 /etc/gitconfig 文件,但只限于 MSys 的根目录下,即安装 Git 时所选的目标位置。

3、配置用户信息

  • 当安装完 Git 应该做的第一件事就是设置你的用户名称与邮件地址。这样做很重要,因为每一个 Git 的提交都会使用这些信息,并且它会写入到你的每一次提交中,不可更改。

    • 注意,在使用 git 命令时,含有空格的词组一般使用双引号(或单引号)包起来。没有空格时可以使用,也可以不使用。

      1
      2
      $ git config --global user.name "Qian Chia"
      $ git config --global user.email "qianchiae@icloud.com"
  • 如果使用了 --global 选项,那么该命令只需要运行一次,因为之后无论你在该系统上做任何事情,Git 都会使用那些信息。当你想针对特定项目使用不同的用户名称与邮件地址时,可以在那个项目目录下运行没有 --global 选项的命令来配置。

4、检查配置信息

  • 如果想要检查你的配置,可以使用以下命令来列出所有 Git 当时能找到的配置。

    1
    $ git config --list
  • 1
    $ git config -l
  • 你可以通过输入 git config [key] 来检查 Git 的某一项配置

    1
    2
    $ git config user.name
    $ git config user.email

5、获取 Git 帮助

  • 若你使用 Git 时需要获取帮助,有三种方法可以找到 Git 命令的使用手册:

    1
    2
    3
    $ git help [verb]
    $ git [verb] --help
    $ man git-[verb]
  • 例如,要想获得 config 命令的手册,执行

    1
    $ git help config
  • 这些命令很棒,因为你随时随地可以使用而无需联网。如果你觉得内容还不够用,你可以尝试在 Freenode IRC 服务器(irc.freenode.ne)的 #git 或 #github 频道寻求帮助。这些频道经常有上百人在线,他们都精通 Git 并且乐于助人。

6、常见问题

6.1 解决 mac 下的命令或 Sourcetree 每次拉取提交都需要输入密码

  • 切换到你的项目的目录执行

    1
    $ git config --global credential.helper osxkeychain
  • 如果遇到执行错误,记住 --global 一定写成 --(两个 -

    1
    error:did you mean`--global`(with two dashes?)
  • 如果不能执行,请先安装

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    # Test for the cred helper
    $ git credential-osxkeychain

    git: 'credential-osxkeychain' is not a git command. See 'git --help'.

    # Download the helper
    $ curl -s -O \
    https://github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain

    # Fix the permissions on the file so it can be run
    $ chmod u+x git-credential-osxkeychain


    # Move the helper to the path where git is installed
    $ sudo mv git-credential-osxkeychain \"$(dirname $(which git))/git-credential-osxkeychain"

    Password: [enter your password]

    # Set git to use the osxkeychain credential helper
    $ git config --global credential.helper osxkeychain
文章目录
  1. 1. 1、Mac Git 配置文件
  2. 2. 2、Windows Git 配置文件
  3. 3. 3、配置用户信息
  4. 4. 4、检查配置信息
  5. 5. 5、获取 Git 帮助
  6. 6. 6、常见问题
    1. 6.1. 6.1 解决 mac 下的命令或 Sourcetree 每次拉取提交都需要输入密码
隐藏目录