-1

I recently setup SSH keys to manage my two git accounts (bitbucket and github). However, whenever I push a commit, the author on those pushes is my computer's name rather than the respective usernames that I use for each account

Here is my config file:

#Github
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/githubssh
  IdentitiesOnly yes

#Bitbucket
Host bitbucket.org
  HostName bitbucket.org
  User git
  IdentityFile ~/.ssh/bitbucketssh
  IdentitiesOnly yes

My pushes are authored by MyName rather than by GithubUserName or BitbucketUserName. Running ssh -T shows that both SSH keys are properly connected.

EDIT: fixed by doing in their corresponding repositories

$ git config user.name = "myusername" 
$ git config user.email = "my email"

However, it looks like I have to do this every time I clone a new repository, is there a way to set it so that I don't have to?

Cris
  • 209
  • 2
  • 10
  • Does this answer your question? [Change email address in Git](https://stackoverflow.com/questions/37805621/change-email-address-in-git) – flyingdutchman Mar 16 '21 at 14:35
  • @flyingdutchman kind of. If you look at my edit I already set my email locally. However I use different emails for each account so I would have to manually set it every time I clone a repo. – Cris Mar 16 '21 at 15:02
  • Note, for future reference, that `git push` works by sending *commits*. The commits are already fully formed, and because they are found via their hash IDs and their hash IDs are constructed by hashing their contents, the contents can never be changed. So by the time you use `git push`, it's far too late to specify the user name and email address for new commits: you're pushing old, existing commits, that cannot be changed. – torek Mar 16 '21 at 17:38

1 Answers1

2

Make a little shell script (let's call it git-be):

#!/bin/sh
persona=$1
. $HOME/.config/git-be/$persona
git config user.name "$PERSONA_NAME"
git config user.email "$PERSONA_EMAIL"

Put this in your $PATH and make it executable.

Create a pair of persona files in ~/.config/git-be:

mkdir -p ~/.config/git-be

cat > ~/.config/git-be/github <<EOF
PERSONA_NAME="GitHub Name"
PERSONA_EMAIL=github_name@example.com
EOF

cat > ~/.config/git-be/bitbucket <<EOF
PERSONA_NAME="BitBucket Name"
PERSONA_EMAIL=bitbucket_name@example.com
EOF

Now from within a repository you can type:

git be github

Or:

git be bitbucket

And you'll be all set.

larsks
  • 277,717
  • 41
  • 399
  • 399