Search code examples
gitgitlabgit-commitgit-filter-branch

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

  • 1)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?

  • 2)Can I do this using only gitlab interface and without messing with my git config on my laptop? (I am asking because I have several git accounts and I need to use different accoounts for my projects)


Solution

  • 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