github ssh 방식, 여러 계정으로 login 및 commit
ssh 방식으로 로그인 하다가, git push가 갑자기 작동하지 않아서 블로그에 다시 기록해둔다.
ssh 정보 등록
위 링크를 참고하면 된다.
요약-
1) ssh key 생성
2) github에 key 등록
3) config에 Host에 -username 을 추가해 줌
4) git clone 시에도 git@github.com-username:username ... 추가
> cd ~/.ssh
> vim config
config에 아래와 같이 적어준다.
> cd ~/.ssh
> ssh-keygen -t rsa -C "mail-a@gmail.com" -f "id_rsa_github_a"
> ssh-keygen -t rsa -C "mail-b@gmail.com" -f "id_rsa_github_b"
이제 cd ~/.ssh 위치에 config 파일이 있고, 아래처럼 user를 나눠서(userA, userB)로 ssh 정보를 기록해놓으면 된다. 실제 ssh key 정보는 id_rsa_userA.pub 또는 id_rsa_userB.pub에 있다.
Host github.com-userA
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/id_rsa_userA
Host github.com-userB
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/id_rsa_userB
github에 .pub 정보를 등록해주면 된다. 예를 들어 회사 계정으로 github에 로그인하고, userB를 회사 계정으로 설정한다면 userB.pub 정보를 아래 링크에 들어가서 ssh-key로 등록해주면 된다.
https://github.com/settings/keys
ssh 주소를 remote에 다시 등록
$ git remote -v
: remote 등록 정보를 확인할 수 있다. 일반적으로 origin 이라는 이름으로 등록한다.
(이미 등록된 경우)
$ git remote remove origin
: origin 이라는 이름의 remote url 정보를 삭제한다.
$ git remote add origin {git clone 주소}
***github.com-userA로 등록한 경우의 git clone 주소는 git@github.com-userA:{repository 이름}.git
github의 <> code 버튼을 누르면 clone 할 수 있는 SSH 주소를 알 수 있다. 이 git clone 주소에 로컬에 저장된 유저 이름을 입력해주면 정상적으로 연결된다. 예를 들어 위에서 github.com-userA로 등록한 경우의 git clone 주소는 git@github.com-userA:{repository 이름}.git 이 된다.
remote를 다시 등록하면 기존에 local 브랜치들의 참조가 바뀔 수 있으므로
$ git pull
명령어로 remote와 sync를 맞춰주는게 좋다.