Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you commit code as a different user?

People also ask

How do I commit to another user?

Creates a new commit by the specified author. Use git commit -m <message> to create a new commit with the specified <message> . Use the --author option to change the <name> and <email> of the commit's author.

Why are my commits linked to the wrong user?

GitHub uses the email address in the commit header to link the commit to a GitHub user. If your commits are being linked to another user, or not linked to a user at all, you may need to change your local Git configuration settings, add an email address to your account email settings, or do both.

How do you commit a code?

To add a Git commit message to your commit, you will use the git commit command followed by the -m flag and then your message in quotes. Adding a Git commit message should look something like this: git commit -m “Add an anchor for the trial end sectionnn.”


Check out the --author option for git commit:

From the man page:

--author=<author>

Override the commit author. Specify an explicit author using the standard A U Thor <[email protected]> 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.


Just to add to this: The --author option mentioned in the accepted answer will only override the author, not the committer information of the commit.

That is the correct behavior in most cases, but if for some reason you need to manually override the committer information as well, use the GIT_COMMITTER_NAME and GIT_COMMITTER_EMAIL environment variables (there is a GIT_COMMITTER_DATE as well). See Git-Internals-Environment-Variables

$ GIT_COMMITTER_NAME="New Name" GIT_COMMITTER_EMAIL="[email protected]" git commit --author="New Name <[email protected]>"

This will make the commit look like it was authored and committed by the specified user.


Use -c option along with git-commit to override any previous configuration. It will not touch your global/project configuration. For example, to override name and email:

git -c user.name='My Name' -c user.email='[email protected]' commit -m "Custom message"

However, if you intend to keep it as an additional setting, I would suggest to use an alias. Edit your ~/.gitconfig file and append a new alias for each non-default user and email.

[user]
  name = My Name
  email = [email protected]

[alias]
  commit-x = -c user.name='My X Name' -c user.email='[email protected]' commit
  commit-y = -c user.name='My Y Name' -c user.email='[email protected]' commit
  commit-z = -c user.name='My Z Name' -c user.email='[email protected]' commit

Alias will be applied globally. Test it.

git commit -m "Custom message with committer and author My Name <[email protected]>"
git commit-x -m "Custom message with committer and author My X Name <[email protected]>"
git commit-y -m "Custom message with committer and author My Y Name <[email protected]>"
git commit-z -m "Custom message with committer and author My Z Name <[email protected]>"