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?
Thanks!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With