怎么分别给公司和个人项目设置 Git User?
source link: https://qq52o.me/2823.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
从第一天使用 Git 开始,每个人都逃不过这个配置:
git config --global user.name Name
git config --global user.email Email
直接给 Git 设置了一个全局用户,或者只有一个项目的时候去掉了 --global
,只在此项目目录下执行设置用户的命令就完事了。
那么问题来了,当电脑中存在多个公司项目和个人项目的时候,使用全局默认的配置,可能一不小心就被钉在了公司项目 Git 历史的耻辱柱上:想象一下,有后人看到这段代码以后,真辣鸡,看看是谁写的,发现是公司邮箱,可能还不知道你,如果你用的 QQ 邮箱...
全局用户在个人项目和公司项目共存的电脑上并不能友好的处理混用的问题,依然需要在每个项目中单独设置。
怎么解决这个问题?
使用 includeIf
includeIf
是 Git 配置系统中的一个功能,它允许你在满足某些条件时自动包含一组配置设置。这个功能特别适合在单个机器上处理不同的工作 / 项目环境。
使用的前提是需要对文件夹的设计有一个简单的规划,举个栗子:
我的方式是在用户根目录 ~
下创建两个目录。
- GitHub 此目录下只放个人项目和开源项目
- workspace 此目录下存放公司项目
在根目录下再创建一个 gitconfig
目录,用于存储一些配置文件,同时也可以增加版本管理,便于迁移。
mkdir ~/gitconfig && cd $_
进入目录并创建下列文件
.gitconfig_company
[user]
name = lufei
email = [email protected]
.gitconfig_individual
[user]
name = sy-records
email = [email protected]
.gitignore_global
.idea
.vscode
.env
vendor/
composer.lock
test.php
node_modules/
.DS_Store
.gitconfig
[user]
name = sy-records
email = [email protected]
[core]
excludesfile = ~/gitconfig/.gitignore_global
[includeIf "gitdir:~/GitHub/"]
path = ~/gitconfig/.gitconfig_individual
[includeIf "gitdir:~/workspace/"]
path = ~/gitconfig/.gitconfig_company
解释一下这个文件的内容,从下往上看:
workspace
目录下的项目使用~/gitconfig/.gitconfig_company
文件中的公司相关的配置;GitHub
目录下的项目使用~/gitconfig/.gitconfig_individual
文件中的个人相关的配置;- 全局的忽略文件放在
~/gitconfig/.gitignore_global
文件中,这样就不用每个项目都创建.gitignore
; - 定义默认的全局用户。
这样只需要把项目放对目录,就再也不会混乱了,即便没有放到 GitHub
和 workspace
目录中,也有默认的全局用户兜底,不至于出现奇奇怪怪的用户的提交。
还没完,创建完成后需要给 Git 的全局配置文件 ~/.gitconfig
中只需要加上下面这一行:
[include]
path = ~/gitconfig/.gitconfig
如果给您解决燃眉之急或带来些许明朗,您可以打赏一杯咖啡或一杯香茗
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK