设置 Git 和相应的配置文件

在安装 Git 之后,我们需要先配置 git,然后才能开始使用它。

有 3 个文件存储 git 配置设置。
这些文件实际上存储相同的信息,但在不同的范围、repo 范围、用户范围和机器级别范围:

  • /etc/gitconfig- 机器级别
  • ~/.gitconfig或者 ~/.config/git/config- 用户级别
  • 我们当前使用的任何存储库的 Git 目录(即.git/config)中的配置文件。特定于该单个存储库。Repo级别

注意:这些位置在 Windows 机器上会有所不同。

这些配置文件具有覆盖功能,其中 repo 级别设置具有最高权限,其次是用户级别(又名全局),最后是机器级别。

我们可以直接从命令行读取/编辑这些文件。
例如,要获取我们执行的所有整体活动 git 设置的列表:

[root@localhost ~]# git config --list        # this lists settings after all over-rides are evaluated.
fatal: error processing config file(s)
[root@localhost ~]# git config --list --global
fatal: unable to read config file '/root/.gitconfig': No such file or directory
[root@localhost ~]# git config --list --system
fatal: unable to read config file '/etc/gitconfig': No such file or directory
[root@localhost ~]#

如我们所见,上述所有命令都失败了。
那是因为这些配置文件都不存在。
但是,它们是在从命令行应用设置时生成的。
在开始使用 git 之前需要设置的两个主要设置是姓名和电子邮件地址。
Git 需要这些信息,以便 git 用这些详细信息标记所有提交,以便每个人跟踪谁提交了什么。
我们可以像这样设置名称和电子邮件地址:

[vagrant@localhost ~]$ git config --global user.name "Sher Chowdhury"
[vagrant@localhost ~]$ git config --global user.email "Sher.Chowdhury@sherc.sg-host.com"
[vagrant@localhost ~]$

运行上述命令会在存储这些信息的用户主目录中生成以下文件:

[vagrant@localhost ~]$ cat .gitconfig
[user]
        name = Sher Chowdhury
        email = Sher.Chowdhury@sherc.sg-host.com
[vagrant@localhost ~]$

现在我们可以从命令行查看此信息:

[vagrant@localhost ~]$ git config --list
user.name=Sher Chowdhury
user.email=Sher.Chowdhury@sherc.sg-host.com
[vagrant@localhost ~]$ git config --list --global
user.name=Sher Chowdhury
user.email=Sher.Chowdhury@sherc.sg-host.com
[vagrant@localhost ~]$

请注意,如果我们省略“全局”范围设置,则 git 将自动在 repo 级别应用该设置(如果存在),否则将失败:

[vagrant@localhost ~]$ git config user.name "Sher Chowdhury"
error: could not lock config file .git/config: No such file or directory
[vagrant@localhost ~]$

现在,我们可以在系统级别执行此操作:

[vagrant@localhost ~]$ sudo git config --system user.email "Sher.Chowdhury@sherc.sg-host.com"
[vagrant@localhost ~]$ cat /etc/gitconfig
[user]
        email = Sher.Chowdhury@sherc.sg-host.com
[vagrant@localhost ~]$ git config --list --system
user.email=Sher.Chowdhury@sherc.sg-host.com
[vagrant@localhost ~]$
日期:2020-07-07 20:54:31 来源:oir作者:oir