Manage multiple Git configs

December 31, 2021

If you use Git, chances are you are using global Git config (config that applies to all repos you create) on your machine. Sometimes, you may want to set up a different Git config for a one-off repo on that machine and this tutorial will walk you through doing that using a personal Github token. I find it simpler to use a token rather than going through separate SSH configs.

First check what your global config looks like:

git config --global user.name
git config --global user.email

Let’s say you created a Git repo which should be shared on Github under a different account. It’s simple. There are 3 steps to do so:

  1. Change Git config for this particular repo

  2. Create Personal Access Token on Github

  3. Set up Git remote repository with the Personal Access Token included

1. Change Git config for repo

Create Git repo and go to that folder. Then use these commands to define different git config for that repo:

Init git in this repo by executing git init

git config user.name "John Doe"
git config user.email "johndoe@example.com"

If you have a personal access token in Github already, you can copy it and skip the next step.

2. Create Personal Access Token

Open https://github.com/settings/tokens on Github or navigate to Developer > Settings > Personal access tokens

Create a new token for repo purposes. It is a good practice to set an expiration date for your token. Copy the token value. You will need it in the next step.

3. Set up remote repository with token

  1. On your machine, add your code to Git and commit.

  2. Configure remote git repo with token from previous step (assuming you are pushing to previously created repo Repo-Name on Github)

If git remote has not been set up for this repo, do so:

git remote add origin https://your-token@github.com/github-username/Repo-Name.git

Note: if git remote has already been set up for this repo but needs to be updated to include the token:

git remote set-url origin https://your-token@github.com/github-username/Repo-Name.git

Create empty Repo-Name under your github.com account.

Now when you push your repo to remote, it will go under github.com/github-username

Bonus

If you already pushed some commits under a wrong Github profile, you can change history of those commits and only rewrite author value.

First check commit hash(es) affected:

git log

Change author for those commits:

git rebase -i my-commit-hash -x "git commit --amend --reset-author -CHEAD"

And finally, force-push the amended commits:

git push origin master --force
Previous
Previous

Bottom Navigation in Jetpack Compose

Next
Next

Jetpack Navigation: Bottom Nav and Multiple Backstacks