DEV Community

Salim MAHBOUBI
Salim MAHBOUBI

Posted on

How to attribute another author when you're committing on git?

Why?

You might find yourself in a situation where you have to commit some code that has been authored by someone else. They might not have access to the repo, or they might be accustomed to Bazaar/SVN/Mercurial but not git, or maybe they're just on a 3 years vacation.
If you commit with the default global or project config, git will present you as the author.
You should not want that to happen for two reasons:
1- You might take credit for someone else's work and that's unethical.
2- You might take blame for their shitty code and that's bad.

How?

For a new commit attributing another person, use this command:

git commit --author="Ms. Jane Codeldoe <Jane@Codeldoe.com>" -m "Jane wrote this I'm just committing"

How it looks on GitHub:
Screenshot showing that a commit on Github

For older commits, it's a bit more complicated, you want to avoid being in this kind of situations.
But, there are solutions, you just need to do some good old googling (or Binging, or Duckduckgoing, whatever suits you).
Solutions might vary depending on whether the commit is pushed to remote or not.
Solutions also vary depending on the git hosting service you're using, I've seen GitHub specific solutions that might not work elsewhere.

P.S
If you're stuck with this kind of problem, or any other problem you think I might be able to help solve, please contact me at msalim [at] boun.cr . I'll be glad to help with my humble know-how.

Top comments (11)

Collapse
 
jpgoldberg profile image
Jeffrey Goldberg

This is why we sign commits!

Collapse
 
ferricoxide profile image
Thomas H Jones II

We do signed commits, as well. Presumably, if you were wanting to do this type of thing (giving credit where credit's due), you'd want to disable signing for the commit.

That said, no matter who the original code-author is, (at least in our organization) if you push breakage into the project because you were effectively vouching for someone else's code, you'll get the blame. Similar rule applies to designated approvers that approve a merge to a protected branch.

Collapse
 
ztickm profile image
Salim MAHBOUBI

Oh this is great thanks!

Collapse
 
defman profile image
Sergey Kislyakov
Collapse
 
gedw99 profile image
Gerard Webb

This is just what I need but did not know was possible. Thanks Salim.

I am not very confident in my team and so if I can submit as a different person ( it's a really big company so will be cool ) and they hate my PR then nothing lost !!.ah but what if they like it ?
Ah it's a double edged sword this one ..

Collapse
 
ferricoxide profile image
Thomas H Jones II

Take note of the doubled commit-icon in the image-capture from the author's repository view. Git still knows who actually pushed the commite - eitehr via the SSH key used for the git@... URL, the credentials passed via the https://... URL or the API-token you used. So, you'll get this doubled icon when the author-tag doesn't match what's in the pusher's registered list of email addresses.

Also, (especially) if you're in a big org, you should probably be forcing commit-signing on your projects to help prevent attempts at game-playing.

Collapse
 
andy profile image
Andy Zhao (he/him)

Woah, that's pretty cool. Did not know that was a feature of git.

Collapse
 
ztickm profile image
Salim MAHBOUBI

Yeah it's really great. The contributors thought of a lot of cases that might not happen to everyone but are worth implementing in case we need it.

Collapse
 
sofianemias profile image
Sofiane

thanks bro !