Git Commit Impersonation
Today at work, I reconfigured a MacBook Pro laptop shared among the team. Many of us have 3 development boxes (Windows, Linux and Mac) but not all. Every now and then, one has to update an Xcode project and/or debug Mac/iOS code. That’s when the laptop comes handy.
Instead of creating a user account per developer though, we decided to go with a single ‘build’ user account. The laptop isn’t a personal development machine anyways. Having a shared account makes it easier to maintain e.g. tmux, Vim or brew configurations. It also saves hard drive space when cloning huge repositories.
Before I had to reconfigure the laptop, we used to remind developers they should use git commit --author=...
e.g. git commit --author='John Doe
<john@doe.com>'
. Which… everyone forgot most of the time and ended up having commits authored by “build@macbookprosdk@example.com”. Not so practical.
Today I decided to tackle this annoyance and I created Git aliases. Now every developer in the team can use her git commit-<initials>
to commit something to the code base in its own name.
By now, when committing some new code, John uses his new shiny initials-enabled commit-jd
alias:
$ git commit-jd -m "fixed problem between chair and keyboard"
To achieve this, add the following excerpt to your global .gitconfig
:
[alias]
commit-jd = "!f() { export GIT_AUTHOR_NAME='John Doe' && export GIT_AUTHOR_EMAIL='john@doe.com' && export GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME && export GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL && git commit \"$@\"; unset GIT_AUTHOR_NAME; unset GIT_AUTHOR_EMAIL; unset GIT_COMMITTER_NAME; unset GIT_COMMITTER_EMAIL; }; f"
At that point, we would like to inhibit the usage of bare git commit
. If Git allowed shadowing commands with aliases, you could even provide a descriptive error message by adding:
[alias]
commit = !f() { echo \"please use your 'commit-<initials>' alias\" }; f"
Git doesn’t allow you to do this, as stated by the git-config manual:
To avoid confusion and troubles with script usage, aliases that hide existing git commands are ignored.
This isn’t a reason to give up, it’s still possible to shadow git commit
from our .bash_profile
:
git() {
if [ x"$1" = xcommit ]; then
echo "please use 'git commit-<initials>'" && false
else
command git "$@"
fi
}
Hope that helps!