thatguyjono
thatguyjono

Reputation: 525

How does GitHub know which account I am using?

I have two GitHub accounts, one is for my work and the other is for my personal use. I want to keep these two separate when I'm doing commits.

Currently, I simply change my GitHub credentials when I can in different repositories on my local system based on whether is it a work/personal repo. Once I am in that repo, I write the following commands to change to that account:

git config --global user.name "myusername"
git config --global user.email [myemail]

Then I handle my git commands and it seems to work fine. I still have a few questions as I feel it's not the correct way to handle things.

  1. If I change my git user/email to my personal account in my work repo and made a commit, will that commit be successful and go through to GitHub? It seems like it does which I don't understand. Would GitHub not check to see if that account has permission to make a commit to that repo?

  2. Can you recommend a better way to handle swapping between my work/personal accounts to keep those two completely separate?

Upvotes: 7

Views: 14904

Answers (2)

wwulfric
wwulfric

Reputation: 531

1)

Github recognize you from your ssh public key. If you didn't submit a public key, github will ask you for your account name and password when you are trying to push code. Check your 'settings -> SSH and GPG keys -> SSH key' in your github profile.

git config --global user.name "myusername"
git config --global user.email [myemail]

The git config content just tell other people what name you want to be called and what email you want to be sent. In fact, you can even leave them blank or fill in any contents.

git config --global user.name "what ever"
git config --global user.email [email protected]

2)

You can try to use different ssh keys. Or simply, you can use different email and name in your .git/config

personal:

[user]
    name = your honey
    email = [email protected]

work:

[user]
    name = your reliable employee
    email = [email protected]

Upvotes: 3

bejado
bejado

Reputation: 1460

If I change my git user/email to my personal account in my work repo and made a commit, will that commit be successful and go through to github?

Yes, but it will be attributed to the wrong email address. See Github help:

If your commits are not linked to any user, we will display the grey Octocat logo beside them

Each commit in git is tagged with an email address. This can be any email address you'd like, as far as GitHub is concerned.

This answer goes over ways to quickly switch between accounts locally. I like the idea of updating the .git/config file in the .git directory of each repo that tells git which email to use:

[user]
    name = John Doe
    email = [email protected]

Add that to the .git/config file for each repository you're working on and git will attribute your commits based on the per-repository email.

If you're wondering how Github keeps track of all this, it is done through something called email aliases.

Upvotes: 4

Related Questions