- Published on
How to manage multiple github repositories with one or more git usernames
- Authors
- Name
- Lorenzo Pieri
- @404answnotfound
The why
This blog is new, the computer(s) on which this blog is written are not. Where does this bring us?
Quite frankly, I was unaware of the ways one could change its own git username by just using a config file, but when I found out, that's where keyboard fury went.
You could argue that I should have known, and I could say that you are right, and yet I never actually needed to change github's username on my git flow because I used to work with a single account until a few days ago.
But that's the past me, the new me is a github account gipsy, roaming through the configs like I was born in them!
The how
First things first, it depends on what you need to do.
I was handling my own repositories from multiuser setup so, of course, my good friend the terminal was trying to push my changes using the local user and that would turn out to be the computer's name, which definately has NOTHING to do with what we are trying to achieve here, so here's the deal:
git config --list
The above command, gently copied on a Terminal window, should output something of this flavor:
# Your git config
user.name=Lorenzo Pieri
user.email=arealemailyoucoulduse@wowthatwasafake.com
color.diff=auto
Now, ain't that so wonderfully normal? But here's the catch
There are 3 different places where you can configure your git configuration (that's a good mouthful, by the way), and those would be:
[path]/etc/gitconfig
~/.gitconfig
or~/.config/git/config
- config file in the Git directory
.git/config
, my new best friend
To do so all you need is a simple yet oh so powerful command
git config --local user.name "Your Name"
git config --local user.email "email@provider.com"
That's it! Your local repository will now cascade (much in the CSS way of things) to the last configuration set, and that would be the local one. This way you will keep all your repos and usernames ready to be committed and pushed by the right folk!
HOLD ON, HOLD ON, HOLD ON. What you say? Fatal error? 403?
Now, that's something.
Oh yeah, that would the origin's fault.
fatal: unable to access 'https://github.com/yourusername/fabolous_repo.git': The requested URL returned error: 403
Solving this is simple, just go to your local .git/config file and change the url value to match your username's authentication
[remote "origin"]
url = https://yourusername@github.com/yourusername/fabolous_repo.git
That's it, for real! Now just git push -u origin main
the s out of it!
The goodies
One of the things that I added to my own .zshrc
, but you can do the same with .bashrc
on unix based OS, is the shell command to change the global git configuration by just typing in a few aliased commands, like so:
alias mygit='git config --global user.name "404answernotfound" \
&& git config --global user.email "404answernotfound@email.com" \
&& git config --list'
alias mysecondgit='git config --global user.name "anotheraccount" \
&& git config --global user.email "anotheraccount@email.com" \
&& git config --list'
This, once sourced
or on a new shell prompt, will allow you switch your global git configuration with ease, just like mygit
or mysecondgit
The goodbye
I hope you found this article useful and to your liking and if you have any requests, drop a message on one of my social media accounts or open an issue/start a discussion on github, on this repository!