I have a work computer, and it\'s configured globally to use my work email and name when committing. This is good. However, I\'d like to make some sort of rule that says, \"if t
There is nothing built into Git to do this (as far as I know), but the following shell script seems to work pretty reliably. Modify it to have the user name and email address you want when checking into GitHub, and then save it as an executable named "git" on your path somewhere before the "real" git.
#!/usr/bin/env bash
# "Real" git is the second one returned by 'which'
REAL_GIT=$(which -a git | sed -n 2p)
# Does the remote "origin" point to GitHub?
if ("$REAL_GIT" remote -v 2>/dev/null |
grep '^origin\b.*github.com.*(push)$' >/dev/null 2>&1); then
# Yes. Set username and email that you use on GitHub.
export GIT_AUTHOR_NAME='*** put your name here ***'
export GIT_AUTHOR_EMAIL='*** put your email address here ***'
fi
"$REAL_GIT" "$@"
I use a similar trick with ssh
on my machine -- I want ssh to change my window background color when it runs, and then change it back when it exits -- and it has worked reliably for me.
Also, note that this is hard-coded to only look at the remote that is named origin
.
I wrote post-checkhout hook to set the repo's local author details based on the repository's origin URL.
It doesn't do wildcard domains, though it makes use of git config --urlmatch
which allegedly will fall back to the closest matching URL.
Check it out here: https://github.com/boywhoroared/dotfiles/blob/master/git/template/hooks/post-checkout.d/author
Git 2.13 adds support for conditional config includes. If you organize your checkouts into directories for each domain of work, then you can add custom settings based on where the checkouts are. In your global git config:
[includeIf "gitdir:code/work/"]
path = /Users/self/code/work/.gitconfig
And then in ~/code/work/.gitconfig:
[user]
email = self@work.com
And of course you can do that for as many domains of work as you like.
Similar to Mike's answer, but thanks to the uniq
command at the fourth line, it will not result in a fork bomb, even if you start a shell inside a shell.
#!/usr/bin/env bash
# "Real" git is the second one returned by 'which'
REAL_GIT=$(which -a git | uniq | sed -n 2p)
# Does the remote "origin" point to GitHub?
if ("$REAL_GIT" remote -v 2>/dev/null |
grep '^origin\b.*github.com.*(push)$' >/dev/null 2>&1); then
# Yes. Set username and email that you use on GitHub.
export GIT_AUTHOR_NAME='*** put your name here ***'
export GIT_AUTHOR_EMAIL='*** put your email address here ***'
fi
"$REAL_GIT" "$@"
user.name
and user.email
are used to "sign" your commits. Since commits are repo-independent (say, the same commit will be in lots of different repos when you push them), those properties are not remote-dependent.
What you can do is to set different logging's when connecting to different remotes via https. You would just put the username in the url, in the form https://username@host/path/to/repo.git
and you're done.
But the commit author's will be the same user.name
, because commits are made locally, and then just shared with others.
As the commiter's identity is part of the commit, if you make two commits that are the same except for the commiter's name and email, those commits would have different hashes, so they'll be two different commits to git at all. That would be a mess :)
If you really want to do this, maybe you can make something with hooks that pushes your commits to another repo on your computer, and that other repo (via hooks, again) will re-write the commits, change the author, and push to the other remote.
But it's so nasty I'll deny having told you about that ;)