专栏名称: SegmentFault思否
SegmentFault (www.sf.gg)开发者社区,是中国年轻开发者喜爱的极客社区,我们为开发者提供最纯粹的技术交流和分享平台。
目录
相关文章推荐
OSC开源社区  ·  Bun ... ·  昨天  
OSC开源社区  ·  RAG市场的2024:随需而变,从狂热到理性 ·  昨天  
程序员的那些事  ·  清华大学:DeepSeek + ... ·  3 天前  
程序猿  ·  “未来 3 年内,Python 在 AI ... ·  5 天前  
程序员小灰  ·  DeepSeek做AI代写,彻底爆了! ·  5 天前  
51好读  ›  专栏  ›  SegmentFault思否

让你的 git 拥有不同身份

SegmentFault思否  · 公众号  · 程序员  · 2018-03-02 08:00

正文

由于你没有进行过特别的设定,所以 git 不管它是往 github 上传还是往你公司的服务器上传,都会以一个完全相同的身份上传,这有时候会造成困扰,比如说这样:

但其实这是我公司的服务器,我不想让它以 fengerzh 的身份上传,我想只有在我往 github 上传的时候才以 fengerzh 上传,而我往公司服务器上传的时候就以 zhangjing 的身份上传,那该怎么做呢?

最直接的方法是在你 git clone 下来的仓库里,有一个 .git 文件夹, .git 文件夹里有一个 config 文件,在这个文件里写上

  1. [user]

  2.    email = [email protected]

  3.    name = zhangjing

就行了。

但问题是我有几十个仓库,不能一个一个设吧,而且万一我忘记了怎么办?所以我们需要有一些自动化的小工具来帮助我们完成这件事情。

首先,你要先建立这么一个文件夹:

  1. mkdir -p ~/.git-templates/hooks

然后你要告诉 git 这个文件夹就是你的模板文件夹:

  1. git config --global init.templatedir ~/.git-templates

再然后,你在这个文件夹里放上一个钩子文件:

  1. vi ~/.git-templates/hooks/post-checkout

这个钩子文件的内容就是下面这样:

  1. #!/bin/bash

  2. function warn {

  3.  echo -e "\n$1 Email and author not initialized in local config!"

  4. }

  5. email= "$(git config --local user.email)"

  6. name="$(git config --local user.name)"

  7. if [[ $1 != "0000000000000000000000000000000000000000" || -n $email || -n $name ]]; then

  8.  exit 0

  9. fi

  10. remote="$([[ $(git remote | wc -l) -eq 1 ]] && git remote || git remote | grep "^origin$")"

  11. if [[ -z $remote ]]; then

  12.  warn "Failed to detect remote."

  13.  exit 0

  14. fi

  15. url="$(git config --local remote.${remote}.url)"

  16. if [[ ! -f ~/.git-clone-init ]]; then

  17. cat << INPUT > ~/.git-clone-init

  18. #!/bin/bash

  19. case "\$url" in

  20.  *@github.com:*    ) email=""; name="";;

  21.  *//github.com/*   ) email=""; name="";;

  22. esac

  23. INPUT

  24.  warn "\nMissing file ~/.git-clone-init. Template created..."

  25.  exit 0

  26. fi

  27. . ~/.git-clone-init

  28. if [[ -z $name || -z $email ]]; then

  29.  warn "Failed to detect identity using ~/.git-clone-init."

  30.  exit 0

  31. fi

  32. git config --local user.email "$email"

  33. git config --local user.name "$name"

  34. echo -e "\nIdentity set to $name "

切记,一定要赋予这个文件可执行权限,否则你的钩子工作不起来:

  1. chmod +x ~/.git-templates/hooks/post-checkout

接下来,你还要再建立另一个文件:

  1. vi ~/.git-clone-init

这个文件的内容是像下面这样:

  1. case "$url" in

  2.  *@github.com:*  ) email="[email protected]";    name="fengerzh";;

  3.  *//github.com/* ) email="[email protected]";    name="fengerzh";;

  4.  *@mydomain.com:*    ) email="[email protected]"; name="zhangjing";;

  5.  *//mydomain.com/*   ) email="[email protected]"; name="zhangjing";;

  6. esac

在这里,我们指明了如果仓库来源是 github 的话我们用哪个用户,如果仓库来源是公司服务器的话又该用哪个用户。

做完了这些事,我们来重新 git clone 一下我们的仓库看看吧:

  1. $ git clone ssh://[email protected]/source/ys.git

  2. Cloning into 'ys'...

  3. remote: Counting objects: 1003, done.

  4. remote: Compressing objects: 100% (591/591), done.

  5. remote: Total 1003 (delta 476), reused 506 (delta 221)

  6. Receiving objects: 100% (1003/1003), 691.97 KiB | 1.71 MiB/s, done.







请到「今天看啥」查看全文