개인 깃 계정과 회사 깃 계정을 분리하여 사용할 상황이 있을 것이다. ssh key와 git config를 설정하면 디렉토리 별로 편하게 유저를 나눌 수 있다.
git config --global --unset user.name
git config --global --unset user.email
실수로 저장소에 사용자 이름, 이메일을 설정하지 않고 작업을 하면 global user의 정보가 들어간다. 따라서 global 정보를 지워 의도치 않은 정보가 들어가지 않게 한다.
git config --global user.useConfigOnly true
글로벌 설정을 지웠고 로컬 저장소에도 사용자 정보가 없으면 git은 시스템 사용자 이름과 호스트 네임으로 사용자 정보를 채워 넣는다. 따라서 사용자 정보가 없을 때 커밋을 막는 설정이 필요하다.
// git 전역 설정이 있는 .gitconfig 파일에 해당 내용을 작성한다.
// 보통 시스템 사용자의 루트 디렉토리에 위치한다.
[includeIf "gitdir:[Dir]/"]
path = [Dir of .gitconfig]
// 실제 내가 쓰는 방법
[includeIf "gitdir:~/Repository/work/"]
path = ~/Repository/work/.gitconfig
[includeIf "gitdir:~/Repository/personal/"]
path = ~/Repository/personal/.gitconfig
[Dir]의 경로 아래의 모든 저장소는 path에 설정된 .gitconfig를 따른다. 내가 쓰는 방법은 내 개인 저장소들을 다룰 디렉토리인 personal과 회사 저장소를 다루는 work 디렉토리로 나눴고 각각의 .gitconfig를 해당 디렉토리 안에 위치시켰다.
각각의 .gitconfig의 사용자 정보에 따라 달라진다.
원격 저장소를 클론하려할 때, https와 ssh 주소를 본 경험이 있을 것이다. ssh 키를 사용하면 원격 저장소와 데이터를 안전하게 주고 받을 수 있고, 이를 이용하여 계정을 분리해보자.
minsu-work, minsu-personal 두 개의 github 계정을 사용한다고 가정한다.
cd ~/.ssh
ssh-keygen -t rsa -b 4096 -C "<github_emial_address>" -f <keyname>
//예시
ssh-keygen -t rsa -b 4096 -C "minsu_work@example.com" -f rsa_minsu_work
ssh-keygen -t rsa -b 4096 -C "minsu_personal@example.com" -f rsa_minsu_personal
예시와 같이 실행하면 각각 keyname에 해당하는 개인키와 .pub으로 끝나는 공개키가 만들어 진다.
ssh-add <keyname>
//예시
ssh-add rsa_minsu_work
ssh-add rsa_minsu_personal
~/.ssh/config를 수정한다. 없으면 만들자
//config
Host github.com-<user id>
HostName github.com
User git
IdentifyFile [ssh key path]
//예시
Host github.com-minsu-work
HostName github.com
User git
IdentyfyFile ~/.ssh/rsa_minsu_work
Host github.com-minsu-personal
HostName github.com
User git
IdentyfyFile ~/.ssh/rsa_minsu_personal
Settings > SSH and GPG keys > new SSH key
.pub으로 끝나는 공개키의 내용을 복사하여 넣는다.
cd ~/.ssh
cat <keyname>.pub
//예시
cat rsa_minsu_personal.pub // sshkey 출력
git clone git@github.com:minsu-work/repositoryA.git
//이미 저장소가 있는 상태에서 적용할 때
git remote set-url origin git@github.com:minsu-work/repositoryA.git
git@github.com:minsu-work/repositoryA.git에서 굵은 글자는 .ssh/config에 작성한 Host와 같다.