[Java Backend Zero to Hello] [Phase 3] BÀI 3.2: GIT NÂNG CAO
📚 Series: Java Backend Zero to Hello 📂 Phân đoạn: Phase 3: Công cụ & Build tools 📖 Nội dung: BÀI 3.2: GIT NÂNG CAO 💡 Khóa học lập trình Backend Java & Spring Boot chuẩn doanh nghiệp từ con số 0.
BÀI 3.2: GIT NÂNG CAO
Mục tiêu
- Sử dụng rebase, stash, cherry-pick
- Giải quyết conflict
- Áp dụng Git workflow phổ biến
- Sử dụng GitHub nâng cao
1. REBASE
Rebase là cách khác để tích hợp thay đổi từ branch khác, tạo lịch sử tuyến tính.
1.1 Merge vs Rebase
Merge: Rebase:
A---B---C (main) A---B---C (main)
\ \
D---E (feature) D'--E' (feature)
1.2 Sử dụng
# Trên branch feature
git rebase main
# Interactive rebase
git rebase -i HEAD~3
1.3 Interactive rebase
git rebase -i HEAD~3
Mở editor:
pick abc1234 Commit 1
pick def5678 Commit 2
pick ghi9012 Commit 3
Các lệnh:
pick- Giữ nguyênreword- Sửa messageedit- Dừng để sửasquash- Gộp với commit trướcdrop- Xóa commit
2. STASH
Lưu tạm thay đổi chưa commit để chuyển branch.
git stash # Lưu tạm
git stash pop # Khôi phục và xóa stash
git stash apply # Khôi phục (giữ stash)
git stash list # Danh sách stash
git stash drop # Xóa stash
git stash clear # Xóa tất cả
# Stash với tên
git stash push -m "WIP: login feature"
# Stash file cụ thể
git stash push <file>
3. CHERRY-PICK
Lấy 1 commit từ branch khác áp dụng vào branch hiện tại.
git cherry-pick <commit-hash>
# Cherry-pick nhiều commit
git cherry-pick <hash1> <hash2>
# Cherry-pick range
git cherry-pick <hash1>..<hash2>
4. GIẢI QUYẾT CONFLICT
4.1 Khi nào xảy ra
- Merge branch có thay đổi cùng dòng
- Rebase
- Pull khi có xung đột
4.2 Cách giải quyết
# 1. Pull/merge/rebase gặp conflict
git status # Xem file conflict
# 2. Mở file, tìm markers:
<<<<<<< HEAD
Code ở branch hiện tại
=======
Code ở branch khác
>>>>>>> branch-name
# 3. Sửa code, xóa markers
# 4. Add và commit
git add <file>
git commit # (cho merge)
git rebase --continue # (cho rebase)
4.3 Công cụ hỗ trợ
git mergetool # Mở tool merge (VS Code, vimdiff...)
git diff --name-only --diff-filter=U # Xem file conflict
git checkout --ours <file> # Giữ code của branch hiện tại
git checkout --theirs <file> # Giữ code của branch merge vào
git reset --hard HEAD # Hủy merge/rebase
5. TAG
Đánh dấu version quan trọng.
git tag # Liệt kê tag
git tag v1.0.0 # Tạo lightweight tag
git tag -a v1.0.0 -m "Version 1.0" # Annotated tag
git tag -d v1.0.0 # Xóa tag local
git push origin v1.0.0 # Push 1 tag
git push origin --tags # Push tất cả tag
6. GIT WORKFLOW PHỔ BIẾN
6.1 Git Flow
master (production)
└── develop
├── feature/*
├── release/*
└── hotfix/*
6.2 GitHub Flow (đơn giản hơn)
main
└── feature branches
6.3 Trunk-Based Development
- Mọi người commit trực tiếp vào main
- Dùng feature flags
- Phù hợp với CI/CD
7. GITHUB NÂNG CAO
7.1 Pull Request (PR)
- Tạo branch, push code
- Vào GitHub → New Pull Request
- Review code, comment
- Merge sau khi approved
7.2 PR Template
Tạo file .github/pull_request_template.md:
## Mô tả
- Thay đổi gì?
## Liên kết
- Issue: #123
## Checklist
- [ ] Code đã test
- [ ] Đã thêm unit test
- [ ] Đã update documentation
7.3 Issue
- Tạo issue để track bug/feature
- Dùng label:
bug,enhancement,documentation - Assign người phụ trách
7.4 GitHub Actions (CI/CD cơ bản)
Tạo .github/workflows/ci.yml:
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK
uses: actions/setup-java@v2
with:
java-version: '17'
- name: Build with Maven
run: mvn clean install
8. GIT ALIAS
Tạo shortcut cho lệnh thường dùng.
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.lg "log --oneline --graph --decorate"
Sử dụng: git st, git co main, git lg
9. BÀI TẬP THỰC HÀNH
Bài 1: Rebase workflow
# Tạo branch feature
git checkout -b feature/api
echo "API code" > api.java
git add . && git commit -m "Add API"
# Quay về main, thêm commit
git checkout main
echo "Main update" > main.java
git add . && git commit -m "Update main"
# Rebase feature vào main
git checkout feature/api
git rebase main
Bài 2: Stash workflow
# Đang làm dở
echo "WIP" > work.java
git add .
# Cần chuyển branch gấp
git stash push -m "WIP work"
# Chuyển branch, làm việc khác
git checkout main
# ...
# Quay lại
git checkout feature/api
git stash pop
Bài 3: Cherry-pick
# Trên branch A có commit quan trọng
git log --oneline
# abc123 Fix critical bug
# Chuyển sang branch B
git checkout branch-b
git cherry-pick abc123
10. TÓM TẮT
| Lệnh | Mô tả |
|---|---|
git rebase |
Tích hợp thay đổi, lịch sử tuyến tính |
git stash |
Lưu tạm thay đổi |
git cherry-pick |
Lấy commit cụ thể |
git tag |
Đánh dấu version |
git mergetool |
Tool giải quyết conflict |
git alias |
Tạo shortcut |
Bài tiếp theo: 3.3 Maven cơ bản
🧭 Điều hướng bài học
- ⬅️ Bài trước: BÀI 3.1: GIT CƠ BẢN
- ➡️ Bài tiếp theo: BÀI 3.3: MAVEN CƠ BẢN
- 📑 Toàn bộ lộ trình: Java Backend Zero to Hello
⭐ Hãy bookmark (clip) lại series để tiện theo dõi các bài học tiếp theo nhé!
All rights reserved