새소식

SCIENCE OF DATA ANALYSIS/데이터 분석을 위한 CS

[GIT] #001 한 컴퓨터 여러 계정 연동하기

  • -

안녕하세요, 데잇입니다! Github에 계정을 연결하는 방법에는 HTTPs, SSH Key방법 두 가지가 있습니다. 여기에서는 그 중 SSH Key방법을 이용해 한 컴퓨터에서 두개 이상의 Github계정을 연동하여 사용하는 설정을 알아봅시다.

SSK Key (로컬)생성 및 (깃헙)지정

(선택) ~/.ssh에 있는 기존 SSH Key를 삭제합니다.

새로운 SSH Key(private & public)를 발급합니다.

ssh-keygen -t rsa -b 4096 -C "account1@gmail.com"  
ssh-keygen -t rsa -b 4096 -C "account2@gmail.com"

이때, 파일명은 각각 ~/.ssh/id_rsa, ~/.ssh/id_rsa_acc2으로 저장합니다.

ssh-agent를 시작하고 ssh에 private key들을 추가합니다.

eval `ssh-agent`  
ssh-add -k ~/.ssh/id_rsa  
ssh-add -k ~/.ssh/id_rsa_acc2  

ssh-add -l로 확인할 수 있고 ssh-add -d ~/.ssh/id_rsa 명령어로 제거할 수 있습니다.

github계정에 pulic key들을 추가합니다.

cat ~/.ssh/id\_rsa.pub | pbcopy  
#Add it on GITHUB>SETTINGS>SSH and GPG keys>New SSH Key  
cat ~/.ssh/id\_rsa\_acc2.pub | pbcopy  
#Add it on GITHUB>SETTINGS>SSH and GPG keys>New SSH Key

config 설정

~/.ssh/config 파일을 아래와 같이 생성합니다.

# account1 github

Host github.com  
HostName github.com  
User git  
AddKeyToAgent yes  
UseKeychain yes
IdentityFile ~/.ssh/id_rsa

# account2 github

Host github.com-acc2  
HostName github.com  
User git  
AddKeyToAgent yes  
UseKeychain yes #재부팅 시 키 설정을 유지해줍니다.
IdentityFile ~/.ssh/id_rsa_acc2
  • git global config를 아래와 같이 설정합니다.  
# for account1  
git config --global user.name "acc2_username"        #github에서 사용하는 이름
git config --global user.email "account2@gmail.com"  
# for account2: nothing to do
  • 로컬 폴더마다 다른 깃헙 계정을 연동하기 위해서는 해당 폴더에서 git init 후 --global flag 없이 위 내용 설정하면 됨
    • If you want to use multiple names and email addresses for different repositories, you can set up local configurations for each repository using the git config command without the --global flag. These local configurations will take precedence over the global configuration for that specific repository.
  • 연결확인
ssh -T git@github.com
ssh -T git@github.com-acc2

Github Repository 연결

ssh config를 반영하여 ssh주소를 연결합니다.

# github.com-acc2: githubHost, acc2_username: github id  

# WHEN YOU CLONING YOUR REPO with acc2
git clone git@github.com-acc2:acc2_username/reponame.git

#WHEN YOU MADE A LOCAL FOLDER AND CONNECT IT TO GITHUB with acc2  
git remote add origin git@github.com-acc2:acc2_username/reponame.git    # 생성시
git remote set-url origin git@github.com-acc2:acc2_username/reponame.git # 변경시

git push origin main 명령어를 이용하여 git에 push를 수행합니다.

Contents

소중한 공감 감사합니다

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 댓글/공감 꾸욱!