现在互联网上有众多git源代码托管服务提供商,比较著名的github, aliyun, coding, oschina等; 以及公司对代码的安全策略,禁止代码存放在公司服务器上; 或者参与开源项目, 贡献自己的一份力量。每个人难免会维护多个git账号以区分不同的源代码托管服务商、个人项目、公司项目、开源项目等。
为了方便不同的git账号之间的切换,利用~/.ssh/config对不同的域进行配置 (更多阅读请参考 https://my.oschina.net/csensix/blog/184434 ), 为每个域设置独立的或共享的ssh key, 但用户还需要针对每个git仓库手动设置git config user.name与git config user.email, 其中滋味只有参与的人才能感同身爱。
为避免每次git clone时配置git账号信息, 更为了区分公司邮箱及个人邮件, 编写了两个Shell脚本, 辅助git的操作。
git-msg
当git的提交历史记录中用户名及邮箱配置有误时,可利用此脚本进行更新。
git 最后一次的提交可以直接使用命令
git commit --amend修正。
-
Get
git-msgfrom github
wget https://github.com/tianxiaxi/git-msg/raw/master/git-msg -O /usr/bin/git-msg
chmod +x /usr/bin/git-msg
-
Usage
git-msg - Update user name and email from git history message
Usage: git-msg <old email> <user name> <new email>
Required:
<old email> - Old email which will be replaced
<user name> - User name for git commiter and author
<new email> - New email for git commiter and author
Author: wayne@zanran.me
URL: www.dailyshare.cn
Github: git@github.com:tianxiaxi/git-msg.git
git-user
当git clone之后,git-user从~/.ssh/config中读取相应的配置信息, 并自动设置user.name与user.email。
具体步骤如下:
- 从
<git repo>/.git/config中获取git仓库的主机, 如github.com - 从
~/.ssh/config中找到对应的User及IdentityFile - 从
IdentityFile对应的公钥中解析出对应的email - 根据获取的
user与email配置git的账户信息
-
Get
git-userfrom github
wget https://github.com/tianxiaxi/git-msg/raw/master/git-user -O /usr/bin/git-user
chmod +x /usr/bin/git-user
-
Usage
git-user - Auto update git user name and email form ~/.ssh/config
Usage: git-user [-h | --help | ?]
Optional:
-h | --help | ? - Show usage information for git-user
Author: wayne@zanran.me
URL: www.dailyshare.cn
Github: git@github.com:tianxiaxi/git-msg.git