How to use multi git accounts

  • August 1, 2022
  • 375

Khi làm việc với nhiều dự án mà mỗi dự án lại dùng 1 tài khoản git khác nhau thì phải làm sao? Hôm nay chúng ta cùng tìm cách để thiết lập sử dụng nhiều tài khoản khi làm việc với git nhé. Github chỉ cho phép với một ssh key thì chỉ add được cho 1 tài khoản vậy phải làm sao bây giờ? Dừng lo lắng chỉ cần làm xong các bước sau là sẽ giải quyết được vấn đề trên.

Create ssh key by account

Đầu tiên chúng ta cần tạo các cặp ssh key cho các account


// Truy cập vào nơi chứa file ssh
cd ~/.ssh

// Tạo key cho tài khoản 1
ssh-keygen -t rsa -b 4096 -C "personal@email.com"

// Tạo key cho tài khoản 2
ssh-keygen -t rsa -b 4096 -C "office@email.com"

Sau khi chạy xong command tạo ssh key giờ chúng ta sẽ thấy các file sau được tạo ra


// Liệt kê file trong folder .ssh
ls -al ~/.ssh

// List các ssh key
id_rsa_personal
id_rsa_personal.pub
id_rsa_office
id_rsa_office.pub

// Chmod for public key
chmod -R 600 id_rsa_personal.pub
chmod -R 600 id_rsa_office.pub

Add ssh key to github

Khi đã tạo được 2 key cho 2 account git, chúng ta sẽ thêm key cho tài khoản github Đăng nhập vào tài khoản git và add ssh key cho mỗi tài khoản, nội dung key là nội dung của 2 file


// Key add to github account
id_rsa_personal.pub
id_rsa_office.pub

Edit/Create ssh config file

Thiết lập ssh config cho 2 tài khoản mới


cd ~/.ssh

// Thêm config sau vào file ~/.ssh/config
# Personal account
Host github.com-personal
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_personal

# Office account
Host github.com-office
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_office

# Dùng vi để thêm config vi ~/.ssh/config

Add ssh private keys to your agent


eval "$(ssh-agent -s)"

ssh-add ~/.ssh/id_rsa_personal
ssh-add ~/.ssh/id_rsa_office

Test ssh connect to git


ssh -T git@github.com
ssh -T git@github.com-personal

Use git with multi account

Giờ là lúc tận hưởng sử dụng git với nhiều tài khoản rồi nhé.
Dùng git với tài khoản personal


// Clone code
git clone git@github.com-personal:USERNAME/my-repo.git 

// Config lại user comit
// Truy cập vào project
cd /path/to/project

// Thay đổi email
git config user.email "personal@email.com"

// Thay đổi tên
git config user.name  "personal"

// Init new project
git init
git config user.email "personal@email.com"
git config user.name  "personal"
git add .
git commit -am "init project"
git remote add origin github.com-personal:USERNAME/my-repo.git
git push origin master

Dùng git với tài khoản office


// Clone code
git clone git@github.com-office:USERNAME/my-repo.git 

// Config lại user comit
// Truy cập vào project
cd /path/to/project

// Thay đổi email
git config user.email "office@email.com"

// Thay đổi tên
git config user.name  "office"

// Init new project
git init
git config user.email "office@email.com"
git config user.name  "office"
git add .
git commit -am "init project"
git remote add origin github.com-office:USERNAME/my-repo.git
git push origin master

Như vậy là chúng ta đã dùng được nhiều tài khoản git rồi. A,e có cách nào khác hoặc thắc mắc gì vui lòng comment bên dưới. Thanks for reading...

Nguồn tham khảo
Using multiple github accounts with ssh keys