LoginSignup
34
31

More than 5 years have passed since last update.

githubの複数アカウントの扱い方

Posted at

githubのアカウントを複数持っていて、意図と違うアカウントにpushされちゃってpermission deniedとかになって地味に解消するのに手間取ったので、今後そうならないようにメモっておきます。

陥った現象

個人のアカウントとチームのアカウントを二つ持っていて、とあるアプリケーションをlocalで開発後、githubの個人アカウントにpushしようと思ったら、チームの方にpushされてpermission deniedとかになってしまってpushができない。。。

~/.ssh/configとかAPP_ROOT/.git/configとかを色々いじってみて、ようやく解消したので、そのプロセスをメモしておきます。

githubにsshで接続するために下記を実行

ssh -T git@github.com

すると、認証が完了したときのメッセージが、

Hi {チームのアカウント名}/{APP名}! You've successfully authenticated, but GitHub does not provide shell access.

ここで気づくべきだけど、認証した!と思って、意気揚々とgit pushまでしようとすると、push先がチームのアカウントなのでpermissionがないよと。

対処法

git remoteの先を確認。

git remote -v

=> origin   git@github.com:{個人のアカウント名}/{APP名}.git (fetch)
   origin   git@github.com:{個人のアカウント名}/{APP名}.git (push)

となる。

さっき接続したgithub.comでは、チームのアカウントがpush先になってしまうので、
remoteを書き換える。

vi ~/.ssh/config

に以下の記述を追加する

Host github-mine
  HostName github.com
  User git
  Port 22
  TCPKeepAlive yes
  IdentitiesOnly yes
  IdentityFile ~/.ssh/{秘密鍵の名前}

で、

git remote remove origin
git remote add origin git@github-mine:{個人のアカウント名}/{APP名}.git

とやると、

git remote -v

=> origin   git@github-mine:{個人のアカウント名}/{APP名}.git (fetch)
   origin   git@github-mine:{個人のアカウント名}/{APP名}.git (push)

ってなるので、これでOK!!

git pushが通りましたとさ。

めでたし、めでたし。

34
31
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
31