I use several computers with different gitconfigs. How do I stop leaking my e-mail addresses to GitHub?

余生颓废 提交于 2019-12-08 00:20:44

I've had the same issue for a while: I want different email addresses to be used for work projects, open source projects, personal projects, and so on, and I want it to be set automatically based on folder structure rather than remembering to do it per repo.

This solution will automate the process of setting the email on a per-repo basis. One assumption I'll make is that you have divided your projects into folders based on owner (or rather, based on the email address you want to use). E.g.:

code/
  work/
    (All subfolders use work email.)
  open-source/
    (All subfolders use open source email.)
  personal/
    (All subfolders use personal email.)

According to the Git Docs, post-checkout hooks are run after a git clone command, so we can use a global post-checkout hook to set the email per-repo.

First, do:

git config --global init.templatedir '~/.git_template'

(From Git commit hooks - global settings.)

Then create that folder and a subfolder called hooks. Add the following script as ~/.git_template/hooks/post-checkout:

#!/bin/bash
# Automatically set Git config values from parent folders.

dir=$(pwd)
while [ "$dir" != "/" ]
do
  # First directory would be the new Git repo, so skip it.
  dir=$(dirname $dir)
  if [ -f $dir/.gitconfig ]
  then
    for config in $(git config --file $dir/.gitconfig --list)
    do
      var=$(echo $config | cut -d = -f 1)
      value=$(echo $config | cut -d = -f 2)
      actual=$(git config $var)
      if [ "$actual" != "$value" ]
      then
        git config $var $value
      fi
    done
  fi
done

(You can also find this script at my GitHub repo git-template.

Now, for each of the folders I mentioned, add a .gitconfig file with the appropriate email. Do e.g.:

git config --file /mnt/data/code/open-source/.gitconfig user.email "open-source@scott-weldon.com"

Now, as long as you git clone into the right folder, your repos will automatically have the correct email address set in the local config.

Bonus features:

  1. For those repos you have already cloned, you can just cd to the relevant Git directory and call ~/.git_template/hooks/post-checkout directly to update the email.

  2. You can add whatever config you want to the .gitconfig files, and those settings will also be added to the repo.

Known issues:

  1. Currently it searches the directories from pwd to /, when it should reverse the order. This shouldn't be hard to fix, so I'll add that to my solution at some point.

  2. The script will be executed after every checkout, not just after initial clone, so there may be a small performance penalty. However, the conditionals ensure that it won't set the value if it has already been set, or if no .gitconfig file is found.

  3. Due to #2, there isn't a way to override the settings with repo-specific values.

I think the best solution is what you've said in your third bullet (set author in local configuration in .git/config). This answer address the remaining problem, which is to remember to set this configuration after each new clone.

Assuming those repositories can be expressed by some pattern, the best idea I have is to have a global pre-push hook in each machine that:

  • Check if the remote URL (pre-push hook second parameter) matches the pattern of your public repos.
    • If they match: check if current author email from local configuration is the expected one.
      • If it is OK: continue push.
      • Else: abort push.
    • Else: continue push.

The problem is, even if you had a way to prevent you from using the wrong email address, how is that system supposed to know when you’re using the wrong author information? If you’re doing works stuff from home, how exactly should it know that this is work stuff so your usual home address is wrong? The same applies to the other direction.

The only solution is to remember it yourself, otherwise it just won’t work. So you should set the author information locally for each repository. You could also use hooks, but those also need to be set up individually for each repository, so you’re better off just changing the config (that’s much easier).

You could also set up a new shell alias which sets the HOME environment variable to somewhere else before running the real git command. That way you could switch between two environments depending on the command you run. You could even hide the default git one, so you have to choose explicitly.

Another thing you could do, is to simply clear the author information from your global .gitconfig. That way, Git will always warn you about it. But instead of setting it globally then, just set it individually for each repository. That’s a bit tedious, but at least you get forced to think about which identity you want to choose,

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!