uni farm

同一端末で異なるgithubアカウントにpushするときの手順

同一端末で異なるgithubアカウントにpushするときの手順

個人用、会社用などでgithubアカウントが複数あり、 同じpcから異なるアカウントのリポジトリにpushすると

git push -u origin master
ERROR: Permission to user1/xxx.git denied to user2.

とエラーがでた。その時にやったことを書いておく

ssh keyを作成してgithubに登録

ssh keyの作成

cd ~/.ssh
ssh-keygen -f test -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in test.
Your public key has been saved in test.pub.

githubに登録

githubのページから プロフィールアイコン->settings->SSH and GPG keys->add sshkey->test.pubの中身を登録

~/.ssh/configに接続先を設定

---
# user1 github
Host github-user1
  HostName github.com
  User git
  Port 22
  TCPKeepAlive yes
  IdentitiesOnly yes
  IdentityFile ~/.ssh/test
---

を追記

git remoteの登録

remoteのurlに git@{~/.ssh/configのHostの名前}:{個人のアカウント名}/{APP名}.git を登録

# 登録の場合
git remote add origin git@github-user1:{個人のアカウント名}/{APP名}.git
# 更新の場合
git remote set-url origin git@github-user1:{個人のアカウント名}/{APP名}.git
git remote -v
origin	git@github-user1:{個人のアカウント名}/{APP名}.git (fetch)
origin	git@github-user1:{個人のアカウント名}/{APP名}.git (push)

でpushできるようになる

おまけ(git config —localの設定)

個人用のリポジトリでコミットログに表示されるユーザ名とemailを変えたい時に設定する リポジトリ毎に設定することができる

git config --local user.name "サブアカウント"
git config --local user.email "サブアカウントメールアドレス"

git config --local -l
user.name="サブアカウント"
user.email="サブアカウントメールアドレス"

参考

2024, Built with Gatsby. This site uses Google Analytics.