Bỏ qua

Git - Quy ước sử dụng Git

Git Usage Guidelines

Tài liệu hướng dẫn quy ước sử dụng Git và các best practices trong quá trình phát triển phần mềm.

File PDF: git.pdf

Quy tắc Git Pull với Rebase

Không nên dùng git pull

Để tránh việc tạo ra merge commit và giữ lịch sử commit sạch sẽ, không nên sử dụng git pull trực tiếp.

Quy trình sử dụng git pull --rebase

Bước 1: Sử dụng git pull với rebase

git pull --rebase origin branch_name

Bước 2: Tiến hành fix conflict nếu có

# Sửa các file conflict trong editor

Bước 3: Add các file đã fix conflict

git add <file_đã_fix_conflict>

Bước 4: Tiếp tục rebase

git rebase --continue

Bước 5: Chỉnh sửa commit message (nếu cần)

  • Git sẽ mở editor để bạn sửa commit message
  • Windows: Nhấn Ctrl + X để save và thoát
  • macOS: Nhấn Cmd + X để save và thoát

Bước 6: Push với force

git push --force origin branch_name

Quản lý Branches và Tags

Xóa Branch

# Xóa branch local (an toàn - không xóa nếu chưa merge)
git branch -d branch_name

# Force delete branch local
git branch -D branch_name

# Xóa branch trên remote
git push origin --delete branch_name

Tạo và quản lý Tags

# Tạo tag
git tag v1.0.0

# Tạo annotated tag
git tag -a v1.0.0 -m "Version 1.0.0"

# Push tags lên remote
git push origin --tags

# Xóa tag local
git tag -d v1.0.0

# Xóa tag trên remote
git push origin --delete v1.0.0

Để xóa các file khỏi GitHub repository khi update gitignore (nhưng giữ lại trên máy local)

  1. Remove khỏi git tracking

    git rm --cached assets/css/custom.css
    git rm --cached assets/js/auth.js
    git rm --cached  assets/js/userbar.js
    

  2. git add .gitignore

    git commit -m "Remove asset files from repository, add to .gitignore"
    

  3. Push để xóa khỏi GitHub

    git push origin main
    

  4. Kiểm tra:

    git status
    

  5. Lưu ý:

    • Các file sẽ biến mất khỏi GitHub ngay sau khi push
    • Nếu ai đó clone repo mới, họ sẽ không có các file này
    • File chỉ tồn tại trên máy những người đã pull trước đó

Xử lý lỗi xác thực

Reset Remote với Token

# Xóa remote hiện tại
git remote remove origin

# Thêm remote mới với token
git remote add origin https://token@github.com/phuchung-freelancer/tanson-sathach-web.git

# Hoặc set URL với token
git remote set-url origin https://token@github.com/phuchung-freelancer/tanson-sathach-web.git

Cấu hình Global User

git config --global user.name "phuchung-freelancer"
git config --global user.email "phuchung.freelancer@gmail.com"
Cấu hình nhiều tài khoản Git trên macOS

1. Cấu hình user cho repository

# Cấu hình local cho repository hiện tại
git config --local user.name "John Doe"
git config --local user.email "john.doe@example.com"

2. Tạo SSH Keys

# Tạo SSH key với Ed25519
ssh-keygen -t ed25519 -C "john.doe@example.com"

# Tạo SSH key với RSA và file name tùy chỉnh
ssh-keygen -t rsa -C "john.doe@example.com" -f other_file_save_key

# Khởi động ssh-agent
eval "$(ssh-agent -s)"

# Tạo SSH key mới với RSA 4096-bit
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/my_new_key
ssh-keygen -t rsa -b 4096 -C "phuchung-vo || your_email@example.com"

3. Cấu hình SSH Config

Tạo file ~/.ssh/config:

# GitLab configuration
Host gitlab.com
    HostName gitlab.com
    IdentityFile ~/.ssh/my_new_key
    User git

# GitHub configuration  
Host github.com
    HostName github.com
    IdentityFile ~/.ssh/my_new_key
    User git

4. Test SSH Connection

ssh -T git@github.com

5. Quản lý Multiple Remotes

# Thêm remote cho tài khoản cá nhân
git remote add origin-personal git@github.com:personal-account/repo.git

# Thêm remote cho tài khoản công ty
git remote add origin-work git@github.com:work-account/repo.git

6. Quản lý Remote URLs

# Xem danh sách remote
git remote -v

# Thay đổi URL của remote
git remote set-url origin https://github.com/myusername/myrepo.git

# Đổi tên remote
git remote rename <tên_cũ> <tên_mới>

# Xóa remote
git remote rm remote-name

7. Push/Pull với Multiple Remotes

# Push đến remote cá nhân
git push origin-personal main

# Pull từ remote công ty
git pull origin-work main
Cài Git trên Ubuntu
sudo apt-get update
sudo apt-get install git
git --version
Best Practices
  • Luôn sử dụng git pull --rebase thay vì git pull
  • Cấu hình SSH keys riêng biệt cho từng tài khoản
  • Sử dụng meaningful commit messages
  • Thường xuyên push code để tránh conflict lớn
  • Review code trước khi merge vào main branch