git commit as different user without email / or on

2019-01-12 21:03发布

问题:

I'm trying to commit some changes as a different user, but i do not have a valid email address, following command is not working for me:

git commit --author="john doe" -m "some fix"
fatal: No existing author found with 'john doe'

I have the same problem when trying to commit with only an email address

git commit --author="john@doe.com" -m "some fix"
fatal: No existing author found with 'john@doe.com'

On the git man pages for the commit command it says i can use the

standard A U Thor <author@example.com> format

For the --author option.

Where is this format defined ? what does A and U stand for ? how do i commit for a different user with only a username or only an email?

回答1:

The minimal required author format, as hinted to in this SO answer, is

Name <email>

In your case, this means you want to write

git commit --author="Name <email>" -m "whatever"

Per Willem D'Haeseleer's comment, if you don't have an email address, you can use <>:

git commit --author="Name <>" -m "whatever"

As written on the git commit man page that you linked to, if you supply anything less than that, it's used as a search token to search through previous commits, looking for other commits by that author.



回答2:

The specific format is:

git commit --author="John Doe <john@doe.com>" -m "Impersonation is evil." 


回答3:

The

standard A U Thor <author@example.com> format

Seems to be defined as followed: ( as far as i know, with absolutely no warranty )

A U Thor = required username

  • The separation of the characters probably indicates that spaces are allowed, it could also be resembling initials.
  • The username has to be followed by 1 space, extra spaces will be truncated

<author@example.com> = optional email address

  • Must always be between < > signs.
  • The email address format isn't validated, you can pretty much enter whatever you want
  • Optional, you can omit this explicitly by using <>

If you don't use this exact syntax, git will search through the existing commits and use the first commit that contains your provided string.

Examples:

  1. Only user name

    Omit the email address explicitly:

    git commit --author="John Doe <>" -m "Impersonation is evil."
    
  2. Only email

    Technically this isn't possible. You can however enter the email address as the username and explicitly omit the email address. This doesn't seem like it's very useful. I think it would make even more sense to extract the user name from the email address and then use that as the username. But if you have to:

    git commit --author="john@doe.com <>" -m "Impersonation is evil." 
    

I ran in to this when trying to convert a repository from mercurial to git. I tested the commands on msysgit 1.7.10.



回答4:

Format

A U Thor <author@example.com>

simply mean that you should specify

FirstName MiddleName LastName <email@example.com>

Looks like middle and last names are optional (maybe the part before email doesn't have a strict format at all). Try, for example, this:

git commit --author="John <john@doe.com>" -m "some fix"

As the docs say:

  --author=<author>
       Override the commit author. Specify an explicit author using the standard 
       A U Thor <author@example.com> format. Otherwise <author> is assumed to 
       be a pattern and is used to search for an existing commit by that author 
       (i.e. rev-list --all -i --author=<author>); the commit author is then copied 
       from the first such commit found.

if you don't use this format, git treats provided string as a pattern and tries to find matching name among the authors of other commits.



回答5:

Just supplement:

git commit --author="john@doe.com " -m "Impersonation is evil."

In some cases the commit still fails and shows you the following 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.

fatal: unable to auto-detect email address (got xxxx)

So just run "git config", then "git commit"



回答6:

If you are committing to Github you don't need a real email you can use <username>@users.noreply.github.com

Regardless of using Github or not, you probably first want change your committer details (on windows use SET GIT_...)

GIT_COMMITTER_NAME='username' 
GIT_COMMITTER_EMAIL='username@users.noreply.github.com'

Then set the author

git commit --author="username <username@users.noreply.github.com>"

https://help.github.com/articles/keeping-your-email-address-private



标签: git commit