Skip to content

Instantly share code, notes, and snippets.

@Hansimov
Last active July 11, 2023 07:28
Show Gist options
  • Save Hansimov/a68809441cbd16290bdf98a9b5004fdb to your computer and use it in GitHub Desktop.
Save Hansimov/a68809441cbd16290bdf98a9b5004fdb to your computer and use it in GitHub Desktop.
Use multiple GitHub accounts on same machine

References


Example:

git clone git@github.com-hansimov:Hansimov/turtle-trader.git
git clone git@gist.github.com-hansimov:a68809441cbd16290bdf98a9b5004fdb.git

<repo-local-path>\.git\config:

[core]
  ...
[remote "origin"]
	url = git@github.com-hansimov:Hansimov/turtle-trader.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[user]
	name = Hansimov
	email = <email-address>

C:\Users\xxx\.ssh\config:

Host github.com
    Hostname ssh.github.com
    Port 443
    ProxyCommand connect -H <proxy-server>:<port> %h %p
    IdentityFile ~/.ssh/id_rsa_work
Host github.com-hansimov
    Hostname ssh.github.com
    Port 443
    ProxyCommand connect -H <proxy-server>:<port> %h %p
    IdentityFile ~/.ssh/id_rsa_hansimov
Host gist.github.com-hansimov
    Hostname ssh.github.com
    Port 443
    ProxyCommand connect -H <proxy-server>:<port> %h %p
    IdentityFile ~/.ssh/id_rsa_hansimov
@Hansimov
Copy link
Author

Hansimov commented Apr 18, 2023

A quick way to git push with another account

git clone https://github.com/<REPO_OWNER>/<REPO>.git

Method 1: Run following commands

git config --local user.name COMMITER_NAME

# NOTE: Do not forget the quotes "" around email!
git config --local user.email "COMMITER_EMAIL"

git remote set-url origin https://<COMMITTER_NAME>:<PAT>@github.com/<REPO_OWNER>/<REPO>.git

Method 2: Just modify these settings in .git/config:

[remote "origin"]
    url = https://<COMMITTER_NAME>:<PAT>@github.com/<REPO_OWNER>/<REPO>.git
    fetch = +refs/heads/*:refs/remotes/origin/*
...
[user]
    name = <COMMITTER_NAME>
    email = <COMMITER_EMAIL>

Now we can do some git operations as COMMITER_NAME ...

git add -u
git commit -m "<COMMIT_MESSAGES>"
git push

References:

@Hansimov
Copy link
Author

Hansimov commented May 8, 2023

If already commit with wrong authors, run:

git config --local user.name CORRECT_NAME
git config --local user.email CORRECT_EMAIL
git rebase -i BASE_SHA -x "git commit --amend --author 'CORRECT_NAME <CORRECT_EMAIL>' -CHEAD"

# In rebase UI of vim:
# type `:wq` and enter, which does pick and exec

git push -f

For example, if you use wrong account to add two commits, then BASE_SHA should be HEAD~2.

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment