0% found this document useful (0 votes)
83 views4 pages

1 - How To Configure GIT Credentials For One Repo Instead Globally Set or Manage Multiple Git Configs For Specific Repo

This document discusses how to set up different Git configurations for individual repositories on a machine in order to push commits to different GitHub accounts. It provides steps to change the Git config for a specific repo, create a personal access token on GitHub, and set up the remote repository URL using the token. This allows pushing the repo to the desired GitHub account without affecting other repos configured with a default global Git setup.

Uploaded by

Kashif Kamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views4 pages

1 - How To Configure GIT Credentials For One Repo Instead Globally Set or Manage Multiple Git Configs For Specific Repo

This document discusses how to set up different Git configurations for individual repositories on a machine in order to push commits to different GitHub accounts. It provides steps to change the Git config for a specific repo, create a personal access token on GitHub, and set up the remote repository URL using the token. This allows pushing the repo to the desired GitHub account without affecting other repos configured with a default global Git setup.

Uploaded by

Kashif Kamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

https://www.valueof.

io/blog/multiple-git-configs-personal-github-token

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 "[email protected]"

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://[email protected]/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://[email protected]/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

Prartical [SUCCESS]
% git push --set-upstream origin master
remote: Permission to kkamal123/TestGit.git denied to hatizaz-nisum-com.
fatal: unable to access 'https://github.com/kkamal123/TestGit.git/': The requested URL returned error: 403

TestGit % git config user.name "kkamal123"


TestGit % git config user.email "[email protected]"

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://[email protected]/github-username/Repo-
Name.git

TestGit % git remote set-url origin https://[email protected]/kkamal123/


TestGit.git

Verification

TestGit % cat .git/config


[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://[email protected]/kkamal123/TestGit.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[user]
name = kkamal123
email = [email protected]

You cannot push changes to master branch, so we tried to push changes to feature branch
below.

TestGit % git push --set-upstream origin master


Branch 'master' set up to track remote branch 'master' from 'origin'.
Everything up-to-date

Here, we got success


TestGit % git push --set-upstream origin feature2
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 328 bytes | 328.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'feature2' on GitHub by visiting:
remote: https://github.com/kkamal123/TestGit/pull/new/feature2
remote:
To https://github.com/kkamal123/TestGit.git
* [new branch] feature2 -> feature2
Branch 'feature2' set up to track remote branch 'feature2' from 'origin'.

You might also like