Commit and push to multiple Git accounts on the same computer?

I use GitHub for my personal projects.
I love GitHub.
My portfolio, my Spring Boot project, my Django project… Any personal practice is on my GitHub. (Here.😉)
However, I use GitLab at work.
And sometimes I want to practice some tools and technologies that I need to learn at work but want to commit and push on my personal (GitHub) account on my work laptop!!
How can I manage and use multiple git accounts on the same computer?
Here is what I’ve done and leave this a quick note to myself.
Today’s goal!
Again, here is what I wanted to do:
- Main git account: GitLab (work)
- Sub git account: GitHub (Personal)
And my file directory structure is like this;
1. Setup my main git account
— Use git config global.
I think this is already set up when you build your local environment.
This will be your main git account, so set it globally.
$ git config --global user.name "<main account username>"
$ git config --global user.email "<main account email>"
To check if it’s applied, you run;
$ cat ~/.gitconfig[user]
email = <main account email>
name = <main account username>
2. Setup my sub git account
— Use git config local, set each repository.
To set up your personal git account, you need to set it by a repository.
And set those configs in <repository>/git/config
.
First, clone your personal repo and move to the repo.
$ cd practice/personal_project
$ git config --local user.name "<sub account username>"
$ git config --local user.email "<sub account email>"
To check if it’s applied, you run;
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/yuki-nagano/personal_project.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
[user]
name = <sub account email>
email = <sub account email>
You’re all set!
Now you can commit and push with your main/sub account separatedly!