0
0

More than 1 year has passed since last update.

複数のGithubアカウントを使う

Last updated at Posted at 2022-07-23

ここに全て書いてありますが。。

  
    

秘密鍵と公開鍵を作る

cd ~/.ssh
ssh-keygen -t rsa -C {Githubメールアドレス} -f {作成する鍵の名前}

オプション
-t・・・鍵の種類(RSA)
-C・・・コメント
-b・・・鍵の長さ(2048とか)
-f・・・ファイル名

※もしSourceTreeにSSHキーを読み込ませるには、SSHキーのファイルを「ユーザー名-GitHub.pub」にしないといけない。
   

    
~/.ssh/config を編集します。

Host github.com.main
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_rsa_main
  TCPKeepAlive yes
  IdentitiesOnly yes
Host github.com.sub
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_rsa.sub
  TCPKeepAlive yes
  IdentitiesOnly yes

公開鍵をGitHubに登録

githubに行き、右上のアイコンから
Settings > SSH and GPG keys > New SSH key
に適当にパソコンの名前つけて、公開鍵をコピペ

公開鍵はcatとかで見る

cat ~/.ssh/id_rsa_main.pub

接続確認

ssh -T git@github.com.main
ssh -T git@github.com.sub

successできたらOK。

cloneなど

適当なディレクトリ作って、移動して、

もしmainブランチなら

gti clone git@github.com.sub:username/repository-name.git

もし特定のブランチなら

gti clone -b ブランチ名  git@github.com.sub:username/repository-name.git

後からブランチ追加してチェックアウトして最新にする

git checkout -b ブランチ名 origin/branch
git pull origin ブランチ名

確認など

git branch

その他

#ステージング
git add ファイル名

#コミット
git commit -m "コミットメッセージ"

#push
git push origin ブランチ名

   
    
そういえば~/.gitconfigの設定してなかったけどしなくてもいいのかな。

と思ったらやっぱり必要になったので追記。

gitconfigの設定

もうすでにローカルに存在していたgitの場合、originのurlを変更しないといけなかった。

まず確認

git config remote.origin.url

ローカルのgitのconfigファイルを開き、remote.origin.urlの項目を

remote.origin.url=git@github.com.main:username/repository-name.git

に変更

   
ーーーーーーーーーーーーーーーーーーーー
参考

https://qiita.com/0084ken/items/f4a8b0fbff135a987fea
https://qiita.com/shimotaroo/items/ed08d76447144d566637
https://qiita.com/takuyanin/items/c6a097028a837052c90c
https://qiita.com/naoto_isogai/items/39d4c1a6ce592ddd00c8

0
0
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
0
0