Computer >> Hướng Dẫn Máy Tính >  >> Hệ Thống >> Linux

Cách thiết lập Git username và email trong Ubuntu

Git là hệ thống kiểm soát phiên bản phân tán do Linus Torvalds phát triển để xây dựng kernel Linux. Từ ngày đầu, Git phát triển khổng lồ trở thành hệ thống version control phổ biến nhất.

Git cho phép nhiều user cùng đóng góp dự án, theo dõi thay đổi, revert về phiên bản trước, tạo branch cho phiên bản dự án khác nhau. Đó là lý do quan trọng thiết lập username và email trong Git để mỗi commit trace về user.

Bản chất hướng dẫn này là đi qua cơ bản bắt đầu Git sau cài, đặc biệt thiết lập username và email trong Git.

Lưu ý: dù dùng Ubuntu cho tutorial, bước áp dụng bất kể OS bạn dùng.

Cách cấu hình Global Git Username và Email

Sau cài Git, cần đặt username và email. Git cho phép set global username/email dùng cho tất cả project git hoặc local credentials dùng cho repository cụ thể.

Để set git credentials, dùng lệnh git config. Git config là công cụ built-in cho phép xem và set biến git.

Trong Ubuntu, biến cấu hình git ở thư mục:

  • /etc/gitconfig – File lưu cấu hình git cho tất cả user và repository.
  • ~/.gitconfig – File .gitconfig trong home directory; lưu cấu hình git cho user cụ thể.
  • .git/config – Lưu cấu hình git cho local repository.

Để kiểm tra cấu hình username/email, dùng lệnh:

git config --list

Nếu không có output từ lệnh trên, set username/email.

Để set global commit username/email, nhập lệnh:

git config --global user.name "Username"
git config --global user.email example@email.com

Sau khi lệnh chạy thành công, kiểm tra biến đã set:

git config –list

Sau chạy lệnh, bạn nhận output tương tự:

user.name=Username
user.email=example@email.com
Cách thiết lập Git username và email trong Ubuntu

Bạn cũng chỉnh file git config thêm username/password. Dùng text editor yêu thích, chỉnh "~/.gitconfig" thêm username/email.

nano /home/ubuntu/.gitconfig

Thêm entry như:

[user]
name = Username
email = example@email.com

Lưu file và dùng git config xác minh thêm entry thành công.

Cách cấu hình Local Git Username và Email

Git cũng cho phép cấu hình local username/email. Dùng local credentials cho repository cụ thể.

Để làm vậy, dùng git config không cờ --global từ bên trong thư mục repository.

Ví dụ:

Điều hướng đến thư mục muốn dùng làm repo:

cd ~/workspace

Tiếp theo, khởi tạo thư mục làm git repository:

git init .

Bên trong repository, dùng lệnh dưới set username/email:

git config user.name "localusername"
git config user.email "user@ubuntu.local"

Để xác minh thay đổi, dùng lệnh:

cat .git/config

Lệnh trên điều hướng .git directory trong local repo và hiện nội dung config. Git lưu cấu hình repo cụ thể trong .git/config.

Output cho trường hợp này:

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[user]
name = localusername
email = user@ubuntu.local

Để hiện cả global và local settings, dùng git config. Ví dụ output:

user.name=Username
user.email=example@email.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=Localusername
user.email=user@ubuntu.local

Lệnh git config hữu ích

Lệnh git config cũng cho set cài đặt git khác.

Ví dụ, set default git editor:

git config --global core.editor vim

Thay vim bằng editor chọn, như Emacs, nano, v.v.

Bạn cũng đổi tên mặc định branch khởi tạo – mặc định "master".

Dùng lệnh dưới đổi tên default init branch:

git config --global init.defaultBranch initial

Tương tự, thay "initial" bằng tên mong muốn cho init branch.

Kiểm tra tất cả cài đặt như output dưới:

user.name=Username
user.email=example@email.com
core.editor=vim
init.defaultbranch=initial
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=Localusername
user.email=user@ubuntu.local

Kết luận

Git là công cụ tuyệt vời giúp user maintain, contribute, chia sẻ công việc. Để hiệu quả hơn dùng Git, bạn cũng dùng Git Alias cải thiện workflow, hoặc học xóa branch local/remote.