因为你没有进行过特别的设定,因此git
无论它是往github
上传仍是往你公司的服务器上传,都会以一个彻底相同的身份上传,这有时候会形成困扰,好比说这样:git
但其实这是我公司的服务器,我不想让它以fengerzh
的身份上传,我想只有在我往github
上传的时候才以fengerzh
上传,而我往公司服务器上传的时候就以zhangjing
的身份上传,那该怎么作呢?github
最直接的方法是在你git clone
下来的仓库里,有一个.git
文件夹,.git
文件夹里有一个config
文件,在这个文件里写上bash
[user] email = zhangjing@mydomain.com name = zhangjing
就好了。服务器
但问题是我有几十个仓库,不能一个一个设吧,并且万一我忘记了怎么办?因此咱们须要有一些自动化的小工具来帮助咱们完成这件事情。dom
首先,你要先创建这么一个文件夹:ssh
mkdir -p ~/.git-templates/hooks
而后你要告诉git
这个文件夹就是你的模板文件夹:ide
git config --global init.templatedir ~/.git-templates
再而后,你在这个文件夹里放上一个钩子文件:函数
vi ~/.git-templates/hooks/post-checkout
这个钩子文件的内容就是下面这样:工具
#!/bin/bash function warn { echo -e "\n$1 Email and author not initialized in local config!" } email="$(git config --local user.email)" name="$(git config --local user.name)" if [[ $1 != "0000000000000000000000000000000000000000" || -n $email || -n $name ]]; then exit 0 fi remote="$([[ $(git remote | wc -l) -eq 1 ]] && git remote || git remote | grep "^origin$")" if [[ -z $remote ]]; then warn "Failed to detect remote." exit 0 fi url="$(git config --local remote.${remote}.url)" if [[ ! -f ~/.git-clone-init ]]; then cat << INPUT > ~/.git-clone-init #!/bin/bash case "\$url" in *@github.com:* ) email=""; name="";; *//github.com/* ) email=""; name="";; esac INPUT warn "\nMissing file ~/.git-clone-init. Template created..." exit 0 fi . ~/.git-clone-init if [[ -z $name || -z $email ]]; then warn "Failed to detect identity using ~/.git-clone-init." exit 0 fi git config --local user.email "$email" git config --local user.name "$name" echo -e "\nIdentity set to $name <$email>"
切记,必定要赋予这个文件可执行权限,不然你的钩子工做不起来:post
chmod +x ~/.git-templates/hooks/post-checkout
接下来,你还要再创建另外一个文件:
vi ~/.git-clone-init
这个文件的内容是像下面这样:
case "$url" in *@github.com:* ) email="buzz.zhang@gmail.com"; name="fengerzh";; *//github.com/* ) email="buzz.zhang@gmail.com"; name="fengerzh";; *@mydomain.com:* ) email="zhangjing@mydomain.com"; name="zhangjing";; *//mydomain.com/* ) email="zhangjing@mydomain.com"; name="zhangjing";; esac
在这里,咱们指明了若是仓库来源是github
的话咱们用哪一个用户,若是仓库来源是公司服务器的话又该用哪一个用户。
作完了这些事,咱们来从新git clone
一下咱们的仓库看看吧:
$ git clone ssh://git@mydomain.com/source/ys.git Cloning into 'ys'... remote: Counting objects: 1003, done. remote: Compressing objects: 100% (591/591), done. remote: Total 1003 (delta 476), reused 506 (delta 221) Receiving objects: 100% (1003/1003), 691.97 KiB | 1.71 MiB/s, done. Resolving deltas: 100% (476/476), done. Identity set to zhangjing <zhangjing@mydomain.com>
能够看到,已经设置成功了。再来看一下克隆以后生成的配置文件吧:
$ cat ys/.git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true ignorecase = true precomposeunicode = true [remote "origin"] url = ssh://git@mydomain.com/source/ys.git fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master [user] email = zhangjing@mydomain.com name = zhangjing
在这里咱们看到文件末尾自动增长了两行关于身份的配置,有了这两行,咱们不再用担忧push
的时候弄错身份了。
整个原理其实就是利用了git
的三个特性:初始模板、钩子函数和本地配置。在初始模板里咱们设定好了一个钩子函数,这样只要一执行克隆操做,首先git
会把咱们的模板文件里的钩子函数复制到本地仓库里,而后开始执行这个钩子函数,最后根据URL
地址设置咱们的本地配置。
以上这些代码其实并非我写的,而是来源于一个github
项目,感兴趣的同窗能够去这里参观学习。