为工作和个人事务配置 Git
如果你像我一样,在不同的公司工作,有几份副业和一些有趣的项目,你可能会遇到和我一样的问题:
你想为不同的仓库使用不同的配置,比如电子邮件地址。
当然,你随时可以为每个仓库应用一个本地配置,甚至可以使用一些脚本或别名来帮助你。但同样,如果你像我一样,你不会喜欢这样做,因为你会一次又一次地忘记应用它。
条件配置
Git 允许你根据条件(例如当前目录)添加外部配置文件。考虑到这一点,上面的问题其实很容易解决。
首先为每个“区域”使用不同的基本目录,例如不同的工作场所和个人物品:
/home/me/
dev-work-a/
dev-work-b/
dev-personal/
然后为每个区域创建一个 .gitconfig 文件:
/home/me/
.gitconfig (your main configuration)
.gitconfig-work-a (specific configuration for work a)
.gitconfig-work-b (specific configuration for work b)
.gitconfig-personal (specific configuration for personal projects)
在您的主 .gitconfig 文件中,您可以有条件地包含其他文件,而不是实际值:
# ~/.gitconfig
[includeIf "gitdir:~/dev-work-a/"]
path = .gitconfig-work-a
[includeIf "gitdir:~/dev-work-b/"]
path = .gitconfig-work-b
[includeIf "gitdir:~/dev-personal/"]
path = .gitconfig-personal
在具体的文件中,你只需定义具体的配置值:
# ~/.gitconfig-work-a
[user]
name = Scooby Doo
email = scooby@worka.com
# ~/.gitconfig-work-b
[user]
name = Scooby Doo, Food-Department
email = scooby@workb.com
# ~/.gitconfig-personal
[user]
name = Scooby
email = me@scoobydoo.com
基本上就是这样。所以现在,当你在 Git 中时,~/dev-personal/bigbang-side-hustle
它会从 中获取你的用户配置.gitconfig-personal
。
还有一件事
当然,你不能只将条件配置用于像姓名和电子邮件这样的用户配置,这只是一个非常常见的用例。但实际上,你可以将它用于任何你喜欢的配置。
还有两个条件,例如gitdir
:
onbranch
允许您根据您所在的分支应用配置gitdir/i
类似gitdir
,但不区分大小写
值得一提的是,它适用于所有平台,并且非常兼容Dotfiles。更多信息
请参阅文档。
玩得开心!
文章来源:https://dev.to/flyingdot/configure-git-for-work-and-personal-stuff-434j