Pro Git (SECOND EDITION)
https://git-scm.com/book/en/v2
Pro Git (SECOND EDITION)
https://git-scm.com/book/zh/v2
You should have to do these things only once on any given computer; they’ll stick around between upgrades.
每台计算机上只需要配置一次,程序升级时会保留配置信息。
1. git config
Git comes with a tool called git config
that lets you get and set configuration variables that control all aspects of how Git looks and operates. These variables can be stored in three different places:
-
[path]/etc/gitconfig
file
Contains values applied to every user on the system and all their repositories. If you pass the option --system
to git config
, it reads and writes from this file specifically. Because this is a system configuration file, you would need administrative or superuser privilege to make changes to it.
包含系统上每一个用户及他们仓库的通用配置。如果在执行 git config
时带上 --system
选项,那么它就会读写该文件中的配置变量。 由于它是系统配置文件,因此你需要管理员或超级用户权限来修改它。
-
~/.gitconfig
or~/.config/git/config
file
Values specific personally to you, the user. You can make Git read and write to this file specifically by passing the --global
option, and this affects all of the repositories you work with on your system.
只针对当前用户。你可以传递 --global
选项让 Git 读写此文件,这会对你系统上所有的仓库生效。
-
config
file in the Git directory (that is,.git/config
) of whatever repository you’re currently using
Specific to that single repository. You can force Git to read from and write to this file with the --local
option, but that is in fact the default. Unsurprisingly, you need to be located somewhere in a Git repository for this option to work properly.
针对该仓库。你可以传递 --local
选项让 Git 强制读写此文件,虽然默认情况下用的就是它。当然,你需要进入某个 Git 仓库中才能让该选项生效。
On Windows systems, Git looks for the .gitconfig
file in the $HOME
directory (C:\Users\$USER
for most people). It also still looks for [path]/etc/gitconfig
, although it’s relative to the MSys root, which is wherever you decide to install Git on your Windows system when you run the installer.
在 Windows 系统中,Git 会查找 $HOME
目录下 (一般情况下是 C:\Users\$USER
) 的 .gitconfig
文件。Git 同样也会寻找 [path]/etc/gitconfig
文件,但只限于 MSys 的根目录下,即安装 Git 时所选的目标位置。
If you are using version 2.x or later of Git for Windows, there is also a system-level config file at C:\Documents and Settings\All Users\Application Data\Git\config
on Windows XP, and in C:\ProgramData\Git\config
on Windows Vista and newer. This config file can only be changed by git config -f <file>
as an admin.
You can view all of your settings and where they are coming from using:
$ git config --list --show-origin
(base) yongqiang@yongqiang:~$ git config --list --show-origin
file:/home/yongqiang/.gitconfig user.email=chengyq******@163.com
file:/home/yongqiang/.gitconfig user.name=chengyq******@163.com
file:/home/yongqiang/.gitconfig core.editor=vim
(base) yongqiang@yongqiang:~$
1.1. Your Identity (用户信息)
The first thing you should do when you install Git is to set your user name and email address. This is important because every Git commit uses this information, and it’s immutably baked into the commits you start creating (它们会写入到你的每一次提交中,不可更改):
$ git config --global user.name "Yongqiang Cheng"
$ git config --global user.email "chengyq******@163.com"
Again, you need to do this only once if you pass the --global
option, because then Git will always use that information for anything you do on that system. If you want to override this with a different name or email address for specific projects, you can run the command without the --global
option when you’re in that project.
如果使用了 --global
选项,那么该命令只需要运行一次,因为之后无论你在该系统上做任何事情, Git 都会使用那些信息。当你想针对特定项目使用不同的用户名称与邮件地址时,可以在那个项目目录下运行没有 --global
选项的命令来配置。
本地计算机中 Git 初始设置,设定使用者的 Email 和 Name。
- 设置姓名和邮箱地址
$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "your_email@example.com"
-
~/.gitconfig
文件内容
[user]
name = Firstname Lastname
email = your_email@example.com
-
git config --list
查看当前设定
$ git config --list
user.name=Firstname Lastname
user.email=your_email@example.com
- 想更改这些信息时,可以直接编辑这个设置文件
strong@foreverstrong:~$ git config --global user.name "your_email"
strong@foreverstrong:~$ git config --global user.email "your_email@163.com"
strong@foreverstrong:~$
strong@foreverstrong:~$ cat ~/.gitconfig
[user]
email = your_email@163.com
name = your_email
strong@foreverstrong:~$
--global
参数是全局 (global) 设定。如果需要帮设定不同的作者,可以在特定目录下进行 Git 设定的时候,加上 --local
参数:
$ git config --local user.name "Firstname Lastname"
$ git config --local user.email "your_email@example.com"
在特定目录下进行操作的时候,就会使用 --local
设定的使用者 Email 和 Name 来进行操作。离开这个特定目录后的 Git 操作,还是会使用预设的 --global
设定。
1.2. 提高命令输出的可读性
将 color.ui
设置为 auto
可以让命令的输出拥有更高的可读性。
$ git config --global color.ui auto
~/.gitconfig
文件内容
[color]
ui = auto
strong@foreverstrong:~$ git config --global color.ui auto
strong@foreverstrong:~$
strong@foreverstrong:~$ cat ~/.gitconfig
[user]
email = your_email@163.com
name = your_email
[color]
ui = auto
strong@foreverstrong:~$
- Please tell me who you are.
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git commit -s
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: empty ident name (for <yongqiang@yongqiang.>) not allowed
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git config --global user.email "***@163.com"
yongqiang@yongqiang:~/yongqiang_work/kmeans$ git config --global user.name "***@163.com"
yongqiang@yongqiang:~/yongqiang_work/kmeans$
yongqiang@yongqiang:~/yongqiang_work/kmeans$ cat ~/.gitconfig
[user]
email = ***@163.com
name = ***@163.com
yongqiang@yongqiang:~/yongqiang_work/kmeans$
References
https://yongqiang.blog.csdn.net/
1.3. Your default branch name
By default Git will create a branch called master
when you create a new repository with git init
. From Git version 2.28 onwards, you can set a different name for the initial branch.
To set main
as the default branch name do:
$ git config --global init.defaultBranch main
1.4. Checking Your Settings (检查配置信息)
If you want to check your configuration settings, you can use the git config --list
command to list all the settings Git can find at that point:
$ git config --list
user.name=Yongqiang Cheng
user.email=chengyq******@163.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto
...
You may see keys more than once, because Git reads the same key from different files ([path]/etc/gitconfig
and ~/.gitconfig
, for example). In this case, Git uses the last value for each unique key it sees.
You can also check what Git thinks a specific key’s value is by typing git config <key>
(检查 Git 的某一项配置):文章来源:https://www.toymoban.com/news/detail-841958.html
(base) yongqiang@yongqiang:~$ git config user.name
Yongqiang Cheng
(base) yongqiang@yongqiang:~$
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] 配置 Git 默认编辑器为 Vim, https://yongqiang.blog.csdn.net/article/details/130233565
[3] (日) 大塚弘记 著, 支鹏浩, 刘斌 译. GitHub 入门与实践[M]. 北京:人民邮电出版社, 2015. 1-255
[4] 为你自己学 Git, https://gitbook.tw/
[5] Git, https://git-scm.com/文章来源地址https://www.toymoban.com/news/detail-841958.html
到了这里,关于First-Time Git Setup (初次运行 Git 前的配置)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!