不设置用户名和电子邮件的提交

我试着这样做:

git commit --author='Paul Draper <my@email.org>' -m 'My commit message'

但我得到了:

*** Please tell me who you are.


Run


git config --global user.email "you@example.com"
git config --global user.name "Your Name"


to set your account's default identity.
Omit --global to set the identity only in this repository.

我可以设置它们,但是我在一个共享的盒子上,我必须(想)事后取消它们:

git config user.name 'Paul Draper'
git config user.email 'my@email.org'
git commit -m 'My commit message'
git config --unset user.name
git config --unset user.email

一个 commit台词也太多了吧!

有捷径吗?

41752 次浏览

(This occurred to me after suggesting the long version with the environment variables—git commit wants to set both an author and a committer, and --author only overrides the former.)

All git commands take -c arguments before the action verb to set temporary configuration data, so that's the perfect place for this:

git -c user.name='Paul Draper' -c user.email='my@email.org' commit -m '...'

So in this case -c is part of the git command, not the commit subcommand.

You can edit the .git/config file in your repo to add the following alias :

[alias]
paulcommit = -c user.name='Paul Draper' -c user.email='my@email.org' commit

or you can do this by command line :

git config alias.paulcommit "-c user.name='Paul Draper' -c user.email='my@email.org' commit"

And then you can do :

git paulcommit -m "..."

Remarks:

  • The idea is then to add also aliases like jeancommit, georgecommit, ... for the other users of this shared box.
  • You can add this alias to your global config by editing your personal .gitconfig or by adding --global option to the command line when adding the alias.
  • The alias paulcommit is not a short one, but it is verbose and you can in general type only git pau+tab.

It is worth noting, that git on linux will default to the linux user name as given in the "gecos" field in /etc/passwd. You can output this name with grep $(whoami) /etc/passwd | cut -d ':' -f 5. If this field is empty you cannot commit without setting a user name but if it is not empty git will use it. So you'd have yourself as author and the system user as committer.

See https://github.com/git/git/blob/master/ident.c

Here's the solution that I use on our shared box. In ~/.bashrc, add the following lines:

alias gitaspaul='alias git="git -c user.name=\"Paul Draper\" -c user.email=\"my@email.org\""'
alias gitasnone='unalias git'

It works as follows: when you're in a shell session, run gitaspaul. From that moment on, every git command until the end of the session will use your temporary credentials. When you leave the shell, the credentials are forgotten. If you want to manually forget the credentials before the end of the session, run gitasnone.

This is a nice solution because it's not specific to any one repo, and it sticks around until a natural end of your computing session (like closing your ssh session).

Here's what it looks like:

$ git commit -m "new stuff"


*** Please tell me who you are.


Run


git config --global user.email "you@example.com"
git config --global user.name "Your Name"


to set your account's default identity.


Omit --global to set the identity only in this repository.


$ gitaspaul
$ git commit -m "new stuff"
[master f6a17cd] new stuff
2 files changed, 28 insertions(+), 6 deletions(-)