user2950593
user2950593

Reputation: 9627

How to change author of git commit using ssh keys

I have created the ssh key and added it to my gitlab account.

Then, I've made a commit:

git add .
git commit -m "w"
git push origin master

enter image description here(https://prnt.sc/q7yc4q)

But as you can see my username when I committed was root(this is because user on my laptop is root

Upvotes: 5

Views: 4753

Answers (2)

CodeWizard
CodeWizard

Reputation: 142074

How do I set the desired username let's call it batman, so instead of root I want batman to be displayed as commit author?

Setting username/email for ALL projects

# Set the username for git commit
git config --global user.name "<your name>"

# Set the emailfor git commit
git config --global user.email "<your email address>"

... I need to use different accounts for my projects

Setting username/email for single projects

# --local is the default so you don't have to add it

# Set the username for git commit
git config --local user.name="<your name>"

# Set the emailfor git commit
git config --local user.email="<your email address>"

Replacing the wrong author in commit (will update all the commits to your user)

git filter-branch -f --env-filter '
    GIT_AUTHOR_NAME="Newname"
    GIT_AUTHOR_EMAIL="newemail"
    GIT_COMMITTER_NAME="Newname"
    GIT_COMMITTER_EMAIL="newemail"
  ' HEAD

Replacing the wrong author in the latest commit

# Update the username and email as explained above
# Re-commit to update the username/email
git commit --amend --no-edit 

Upvotes: 7

zigarn
zigarn

Reputation: 11595

In git, commit authoring is defined by the options user.name and user.email. Please follow this documentation to set them: https://help.github.com/en/github/using-git/setting-your-username-in-git (it doesn't depends on using GitHub or GitLab). GitLab/GitHub will use the committer email to link commit author to GitHub/GitLab account.

SSH or HTTP authentication is only used to check permission to push and have no correlation with commit authoring (unlike centralized VCS like CVS or SVN). So you this implies 2 things:

  • you cannot change it on GitLab side
  • it's not easy to change afterwards

To deal with multiple committer names/emails, better is to define the settings at cloned repository level, or "local", by using the --local option with git config.

To change it afterwards, you need to rewrite history while changing authoring and there are already a lot of information on that on SO.

Upvotes: 1

Related Questions