Bài này gộp 2 chủ đề: Release Management, Fork/Upstream Sync — Agency & Contractor Pattern.
Mục tiêu học tập
- Tạo và quản lý Git tags (annotated vs lightweight)
- Hiểu Semantic Versioning (MAJOR.MINOR.PATCH) và quy tắc áp dụng
- Nắm vững release branch workflow và hotfix flow
- Tự động hóa changelog với conventional-changelog
- Tạo GitHub Releases và ký tags với GPG
- Hiểu triangular workflow (fork ↔ upstream)
- Setup remote
upstreamtrỏ đến client repo - Thực hành sync flow: fetch upstream → rebase → force-push to fork an toàn
- Xử lý conflict khi rebase nhiều commits lên upstream mới nhất
- Nắm bidirectional sync khi client merge back
Phần 1: Release Management
1. Git Tags — Đánh Dấu Version
Hai loại tag
# Lightweight tag — đơn giản, chỉ là pointer tới commit
git tag v1.0.0
# Annotated tag — khuyến nghị cho release — có author, date, message
git tag -a v1.0.0 -m "Release version 1.0.0 — initial stable release"
# Tạo tag cho commit cũ (không phải HEAD)
git tag -a v0.9.0 abc1234 -m "Release v0.9.0"
# Xem danh sách tags
git tag
git tag -l "v1.*" # filter theo pattern
# Xem chi tiết annotated tag
git show v1.0.0
Sự khác biệt quan trọng
Push tags lên remote
# Push một tag cụ thể
git push origin v1.0.0
# Push tất cả tags
git push origin --tags
# Xoá tag local
git tag -d v1.0.0-beta
# Xoá tag remote
git push origin :refs/tags/v1.0.0-beta
# Hoặc:
git push origin --delete v1.0.0-beta
2. Semantic Versioning — MAJOR.MINOR.PATCH
Semantic Versioning (SemVer) là tiêu chuẩn đặt tên version cho phần mềm. Nguồn: semver.org.
Quy tắc tăng version
MAJOR.MINOR.PATCH
MAJOR: tăng khi có breaking change (API không tương thích ngược)
MINOR: tăng khi thêm tính năng mới (tương thích ngược)
PATCH: tăng khi sửa bug (tương thích ngược)
Ví dụ:
1.2.3 → 1.2.4 (fix bug)
1.2.3 → 1.3.0 (thêm feature mới, PATCH reset về 0)
1.2.3 → 2.0.0 (breaking change, MINOR + PATCH reset về 0)
Pre-release và build metadata
# Pre-release (thứ tự: alpha < beta < rc < stable)
v1.0.0-alpha.1
v1.0.0-beta.3
v1.0.0-rc.1
v1.0.0
# Build metadata (không ảnh hưởng thứ tự, chỉ thông tin)
v1.0.0+build.42
v1.0.0-beta.1+sha.abc1234
Kết hợp với Conventional Commits
# Conventional Commits giúp automation xác định version bump:
#
# feat: add OAuth login → MINOR bump (1.2.3 → 1.3.0)
# fix: resolve token expiry bug → PATCH bump (1.2.3 → 1.2.4)
# feat!: redesign auth API → MAJOR bump (1.2.3 → 2.0.0)
# BREAKING CHANGE: footer text → MAJOR bump
#
# Commits dạng chore/docs/style → không bump version
3. Release Branch Workflow
Git Flow Release Branch
Tạo và làm việc với release branch
# 1. Tạo release branch từ develop khi feature freeze
git switch develop
git pull origin develop
git switch -c release/1.2.0
# 2. Trên release branch: chỉ sửa bug, cập nhật version number, docs
# KHÔNG thêm feature mới
echo "1.2.0" > VERSION
npm version 1.2.0 --no-git-tag-version # cập nhật package.json
git add VERSION package.json
git commit -m "chore: bump version to 1.2.0"
# 3. Merge vào main khi sẵn sàng release
git switch main
git merge --no-ff release/1.2.0 -m "chore: release v1.2.0"
git tag -a v1.2.0 -m "Release v1.2.0"
git push origin main --tags
# 4. Merge ngược lại develop để sync bug fixes
git switch develop
git merge --no-ff release/1.2.0 -m "chore: sync release/1.2.0 back to develop"
git push origin develop
# 5. Xoá release branch
git branch -d release/1.2.0
git push origin --delete release/1.2.0
4. Hotfix Flow
Hotfix xử lý bug nghiêm trọng trực tiếp trên production.
5. Changelog Tự Động
Conventional Changelog
# Cài đặt
npm install -g conventional-changelog-cli
# Tạo CHANGELOG.md từ lịch sử commits (Conventional Commits format)
conventional-changelog -p angular -i CHANGELOG.md -s
# -p angular: sử dụng Angular preset (feat/fix/perf/docs/...)
# -i CHANGELOG.md: input file
# -s: write output to input file (overwrite nếu cần)
# Tạo từ đầu (toàn bộ lịch sử)
conventional-changelog -p angular -i CHANGELOG.md -s -r 0
Ví dụ CHANGELOG.md được sinh ra
# Changelog
## [1.2.0] - 2024-01-15
### Features
* add OAuth 2.0 integration with Google ([abc1234])
* add dark mode support ([def5678])
### Bug Fixes
* resolve token refresh race condition ([ghi9012])
## [1.1.0] - 2024-01-01
...
Tích hợp vào npm scripts
Lưu ý (2026):
standard-versionđã bị deprecated từ tháng 5/2022. Các thay thế được khuyến nghị:semantic-release(tự động hoàn toàn, phổ biến nhất),release-please(của Google, tạo release PR), hoặccommit-and-tag-version(fork cộng đồng tiếp nối standard-version). Ví dụ dưới đây minh hoạ cơ chế hoạt động — không dùng standard-version cho project mới.
{
"scripts": {
"release": "standard-version",
"release:minor": "standard-version --release-as minor",
"release:major": "standard-version --release-as major"
}
}
# standard-version tự động:
# 1. Tính version bump dựa trên commits
# 2. Cập nhật package.json version
# 3. Tạo/cập nhật CHANGELOG.md
# 4. Tạo commit "chore(release): v1.2.0"
# 5. Tạo annotated tag v1.2.0
npm run release
6. GitHub Releases
GitHub Releases mở rộng Git tags với release notes, binary assets, và link tải xuống.
# Tạo GitHub Release qua CLI (gh)
gh release create v1.2.0 \
--title "Version 1.2.0 — OAuth & Dark Mode" \
--notes-file RELEASE_NOTES.md \
--target main
# Tải artifact lên release
gh release upload v1.2.0 dist/app-linux.tar.gz dist/app-macos.tar.gz
# Tạo draft release (chưa publish)
gh release create v1.3.0-rc.1 --draft --prerelease
# Xem releases
gh release list
gh release view v1.2.0
Tự động tạo release notes từ PR titles
# .github/release.yml — cấu hình auto-generated notes
changelog:
categories:
- title: "Breaking Changes"
labels: ["breaking-change"]
- title: "New Features"
labels: ["enhancement", "feature"]
- title: "Bug Fixes"
labels: ["bug"]
- title: "Documentation"
labels: ["documentation"]
7. Signed Tags với GPG
Signed tags chứng minh danh tính người tạo release — quan trọng cho security-sensitive projects.
# Kiểm tra GPG key đã có chưa
gpg --list-secret-keys --keyid-format=long
# Nếu chưa có, tạo GPG key mới
gpg --gen-key
# Lấy key ID (dạng ABCD1234EFGH5678)
gpg --list-secret-keys --keyid-format=long | grep sec
# Cấu hình Git dùng GPG key
git config --global user.signingkey ABCD1234EFGH5678
git config --global gpg.program gpg
# Tạo signed tag (-s thay vì -a)
git tag -s v1.2.0 -m "Release v1.2.0 (signed)"
# Verify tag signature
git tag -v v1.2.0
# Output: gpg: Good signature from "Your Name <your@email.com>"
# Tự động ký tất cả tags
git config --global tag.gpgSign true
# Push signed tag (giống push thường)
git push origin v1.2.0
Phần 2: Fork/Upstream Sync — Agency & Contractor Pattern
1. Bối Cảnh: Agency/Contractor Pattern
Tình huống thực tế: Công ty bạn (agency) làm dự án cho client. Client có repo riêng. Workflow:
2. Setup Ban Đầu
# 1. Clone fork của agency (origin tự động được setup)
git clone https://github.com/your-agency/project-repo.git
cd project-repo
# 2. Thêm upstream (client repo)
git remote add upstream https://github.com/client-org/project-repo.git
# 3. Kiểm tra remotes
git remote -v
# origin https://github.com/your-agency/project-repo.git (fetch)
# origin https://github.com/your-agency/project-repo.git (push)
# upstream https://github.com/client-org/project-repo.git (fetch)
# upstream https://github.com/client-org/project-repo.git (push)
# Best practice: chỉ fetch từ upstream, không push thẳng vào upstream
# (PR mới là cách đúng để contribute code lên upstream)
git remote set-url --push upstream DISABLE
# Giờ "git push upstream" sẽ báo lỗi — tránh accident push
# Verify
git remote -v
# upstream https://github.com/client-org/project-repo.git (fetch)
# upstream DISABLE (push)
3. Daily Sync Flow
Mỗi ngày trước khi bắt đầu làm việc:
# Bước 1: Fetch toàn bộ cập nhật từ upstream
git fetch upstream
# remote: Enumerating objects: 23, done.
# remote: Counting objects: 100% (23/23), done.
# From https://github.com/client-org/project-repo
# abc1234..def5678 main -> upstream/main
# xyz9012..uvw3456 develop -> upstream/develop
# Bước 2: Rebase local main lên upstream/main
git switch main
git rebase upstream/main
# Successfully rebased and updated refs/heads/main.
# Bước 3: Sync origin (team fork) với upstream
git push origin main
# Nếu origin đã có divergent history (ai đó push trực tiếp), dùng --force-with-lease:
git push origin main --force-with-lease
# Bước 4: Rebase feature branch đang làm lên main mới nhất
git switch feature/my-task
git rebase main
Tại sao dùng rebase thay vì merge?
# Merge: tạo merge commit mỗi lần sync → lịch sử fork lộn xộn khi PR
#
# git merge upstream/main
#
# * abc1234 Merge remote-tracking branch 'upstream/main' ← xấu, không cần thiết
# * def5678 feat: my feature
# Rebase: lịch sử tuyến tính, PR dễ review
#
# git rebase upstream/main
#
# * def5678 feat: my feature ← commit của bạn nằm trên đỉnh upstream
# * [upstream commits]
4. Triangular Workflow — Push to Fork, PR to Upstream
# Workflow chuẩn:
# 1. Sync (như trên)
# 2. Tạo feature branch
git switch -c feature/new-api-endpoint
# 3. Code và commit
git add . && git commit -m "feat: add /api/v2/users endpoint"
git add . && git commit -m "feat: add pagination to user list"
# 4. Trước khi push, rebase lên upstream mới nhất
git fetch upstream
git rebase upstream/main
# Nếu conflict → xử lý (xem section 5)
# 5. Push feature branch lên fork (origin)
git push origin feature/new-api-endpoint
# 6. Tạo PR từ fork → upstream trên GitHub
gh pr create \
--repo client-org/project-repo \
--head your-agency:feature/new-api-endpoint \
--base main \
--title "feat: add /api/v2/users endpoint with pagination" \
--body "..."
5. Xử Lý Conflict Khi Rebase Nhiều Commits
Khi rebase một feature branch dài lên upstream đã có nhiều commits mới:
git fetch upstream
git rebase upstream/main
# Git rebase nhiều commits — có thể conflict ở từng commit:
# CONFLICT (content): Merge conflict in src/api/users.ts
# error: could not apply a1b2c3d... feat: add /api/v2/users endpoint
# Xem conflict hiện tại đang ở commit nào
git status
# rebase in progress; onto upstream/main
# Currently rebasing branch 'feature/new-api-endpoint' on 'abc9876'
# Resolve conflict (xem bài day-03)
vim src/api/users.ts
git add src/api/users.ts
git rebase --continue
# Nếu conflict tiếp tục ở commit tiếp theo → lại resolve → continue
# ...cho đến hết
# Bỏ qua commit hiện tại (nếu nó đã được upstream xử lý rồi)
git rebase --skip
# Hủy toàn bộ rebase, quay về trạng thái trước
git rebase --abort
Rebase nhiều commits với rerere
# Bật rerere để tái dùng conflict resolution (quan trọng khi rebase thường xuyên)
git config rerere.enabled true
# Lần đầu resolve conflict ở commit A → rerere ghi nhớ
# Lần sau rebase lại, gặp conflict y hệt → rerere tự apply
6. Force-Push to Fork An Toàn
Sau khi rebase, cần force-push để cập nhật fork:
# KHÔNG an toàn — xoá commits của đồng nghiệp nếu họ đã push lên cùng branch
git push origin feature/new-api-endpoint --force
# AN TOÀN — kiểm tra remote-tracking ref trước khi push
git push origin feature/new-api-endpoint --force-with-lease
# Nếu đồng nghiệp đã push lên feature branch đó:
# error: failed to push some refs
# hint: Updates were rejected because the remote contains work that you do
# hint: not have locally.
# → Bạn phải fetch và giải quyết conflict trước
# Cấu hình alias cho tiện
git config --global alias.fpush 'push --force-with-lease'
git fpush origin feature/new-api-endpoint
Khi nào force-push được phép?
OK để force-push:
✓ Branch cá nhân (chỉ mình bạn làm việc trên đó)
✓ Feature branch chưa ai khác check out
✓ Sau khi thông báo team: "Tôi đang rebase + force push feature/X"
KHÔNG force-push:
✗ main, develop, release/* — shared branches
✗ Branch đang có open PR mà reviewer đã để comments theo line number
(rebase thay đổi commit hash → comments bị mất context)
✗ Không có --force-with-lease (luôn dùng --force-with-lease)
7. Bidirectional Sync: Client Merges Back
Khi client accept PR của team bạn vào upstream, fork cần được sync lại:
# Sau khi PR được merged vào upstream/main:
git fetch upstream
git switch main
git rebase upstream/main
# Origin (fork) cần được cập nhật
git push origin main
# Nếu main đã diverge:
git push origin main --force-with-lease
# Xoá branch đã merged (local + remote)
git branch -d feature/new-api-endpoint
git push origin --delete feature/new-api-endpoint
Khi client có hotfix chưa được PR
Đôi khi client push trực tiếp lên upstream mà không qua PR của team:
# Phát hiện: upstream/main có commits mà origin/main không có
git fetch upstream
git log origin/main..upstream/main --oneline
# abc1234 hotfix: patch critical security issue (from client team)
# Sync fork với upstream
git switch main
git rebase upstream/main
git push origin main --force-with-lease
# Rebase tất cả feature branches đang active lên main mới
git switch feature/my-ongoing-work
git rebase main
8. Divergent History và Reflog Recovery
Khi history bị phân kỳ quá nhiều
# Xem mức độ diverge
git log origin/main..upstream/main --oneline | wc -l # upstream ahead bao nhiêu commit
git log upstream/main..origin/main --oneline | wc -l # fork ahead bao nhiêu commit
# Nếu fork có commits không nên có (accident commits trực tiếp vào main):
# Option 1: Reset fork/main về upstream/main
git switch main
git reset --hard upstream/main
git push origin main --force-with-lease
# Option 2: Nếu cần giữ lại vài commits cụ thể từ fork
git log upstream/main..origin/main --oneline # commits chỉ có ở fork — ghi lại hash TRƯỚC khi reset
git reset --hard upstream/main
git cherry-pick <commit-hash>
git push origin main --force-with-lease
Lấy lại commits bị mất qua reflog
# Tình huống: rebase sai → commits "biến mất"
git reflog
# abc1234 HEAD@{0}: rebase (finish): returning to refs/heads/feature/X
# def5678 HEAD@{3}: commit: feat: my important commit ← commit bị mất
# ...
# Khôi phục commit bị mất
git checkout -b recovery/feature-x def5678
# Hoặc cherry-pick:
git cherry-pick def5678
# Nếu rebase bị lỗi hoàn toàn, trở về ORIG_HEAD
git reset --hard ORIG_HEAD
# (ORIG_HEAD được Git set tự động trước khi rebase/merge)
Câu hỏi ôn tập
-
Khi nào nên tăng MAJOR version theo SemVer?
Xem đáp án
Tăng MAJOR khi có breaking change — thay đổi API/interface không tương thích ngược. Ví dụ: đổi tên function public, xoá endpoint, thay đổi request/response format, đổi function signature.
1.2.3 → 2.0.0(MINOR và PATCH reset về 0). Conventional Commits báo hiệu bằngfeat!:,fix!:, hoặc footerBREAKING CHANGE:. -
Sự khác biệt chính giữa annotated tag và lightweight tag là gì?
Xem đáp án
Lightweight tag: chỉ là pointer đến commit, không có metadata riêng — như một branch không di chuyển. Annotated tag: tạo một tag object riêng trong
.git/objects/với tagger name, email, date, message, và có thể GPG-signed.git describeưu tiên annotated tag. Dùng annotated tag cho public releases (có thể verify, có đầy đủ metadata); lightweight cho private/temporary markers. -
Tại sao hotfix phải được merge vào cả
mainlẫndevelop?Xem đáp án
main= production code → cần fix ngay để deploy.develop= integration branch cho next release → nếu không merge hotfix vào, bug sẽ reappear trong release tiếp theo (develop không có fix). Đây là lỗi phổ biến trong Git Flow: chỉ merge vào main và nghĩ xong, nhưng develop vẫn có bug. Luôn merge hotfix branch vào cả hai. -
conventional-changelogcần điều kiện gì trong commit history để hoạt động tốt?Xem đáp án
Commit history phải theo Conventional Commits format (
feat:,fix:,docs:,chore:,BREAKING CHANGE:...). Tool parse commit messages để phân loại và generate changelog. Nếu commit messages không theo format (ví dụ "update stuff", "fix bug"), tool không thể phân loại và changelog sẽ thiếu/sai. Cần enforce bằngcommitlint+ Husky hook. -
Lệnh nào xoá một tag trên remote repository?
Xem đáp án
git push origin --delete v1.0.0-betahoặc cú pháp cũgit push origin :refs/tags/v1.0.0-beta. Nhớ xoá cả local tag:git tag -d v1.0.0-beta. Xoá tag trên remote không tự động xoá tag local và ngược lại — cần làm cả hai bước riêng. -
Tại sao nên disable push URL cho
upstreamremote?Xem đáp án
Để tránh accident push thẳng vào upstream (repo của client). Nguyên tắc: chỉ contribute code qua PR từ fork → upstream, không push trực tiếp.
git remote set-url --push upstream DISABLElàm chogit push upstreambáo lỗi ngay — nhắc nhở bạn phải dùng PR. Vẫn có thểgit fetch upstreambình thường. -
Khi rebase feature branch lên upstream/main gặp conflict ở commit thứ 3, làm gì sau khi resolve?
Xem đáp án
(1) Chỉnh sửa file để resolve conflict, (2)
git add <file>, (3)git rebase --continue. Git sẽ áp dụng commit thứ 3 xong rồi tiếp tục với các commits tiếp theo — có thể gặp thêm conflict ở commit sau. Nếu muốn bỏ qua commit hiện tại (đã được upstream handle rồi):git rebase --skip. Để hủy toàn bộ:git rebase --abort. -
--force-with-leasebảo vệ điều gì so với--force?Xem đáp án
--force-with-leasekiểm tra remote-tracking ref local (ví dụorigin/feature/x) có khớp với remote thực hay không. Nếu đồng nghiệp đã push lên branch đó trong lúc bạn rebase, remote-tracking ref local đã lỗi thời → Git từ chối push.--forceghi đè vô điều kiện, có thể xoá commits của đồng nghiệp mà không biết. -
Sau khi PR được merged vào upstream, bạn phải làm gì để fork đồng bộ?
Xem đáp án
(1)
git fetch upstreamđể cập nhật remote-tracking branches, (2)git switch main && git rebase upstream/mainđể sync local main, (3)git push origin main(có thể cần--force-with-lease) để cập nhật fork, (4)git branch -d feature/my-branch && git push origin --delete feature/my-branchđể dọn branch đã merged. -
Lệnh nào giúp xem commits đang có trong upstream nhưng chưa có trong fork?
Xem đáp án
git log origin/main..upstream/main --oneline — hiện commits có trong upstream/main nhưng chưa có trong origin/main (fork). Nhớ git fetch upstream trước để cập nhật remote-tracking refs. Để xem chiều ngược lại (fork ahead upstream): git log upstream/main..origin/main --oneline.
Bài tập thực hành
Phần 1: Release Management
mkdir release-lab && cd release-lab
git init -b main
git config user.name "Dev" && git config user.email "dev@test.com"
# Tạo vài commits theo Conventional Commits
echo "version: 1.0.0" > package.json
git add . && git commit -m "feat: initial project setup"
echo "login handler" > auth.js
git add . && git commit -m "feat: add OAuth login"
echo "bug fix" >> auth.js
git add . && git commit -m "fix: resolve token refresh bug"
# Tạo annotated tag v1.0.0
git tag -a v1.0.0 -m "Release v1.0.0 — initial stable release"
git show v1.0.0
# Thêm commits sau release
echo "dark mode" > theme.js
git add . && git commit -m "feat: add dark mode support"
# Tạo release branch
git switch -c release/1.1.0
echo "1.1.0" > VERSION
git add . && git commit -m "chore: bump version to 1.1.0"
# Merge vào main và tag
git switch main
git merge --no-ff release/1.1.0 -m "chore: release v1.1.0"
git tag -a v1.1.0 -m "Release v1.1.0"
# Xem toàn bộ graph
git log --oneline --graph --all
# Simulate hotfix
git switch -c hotfix/1.1.1
echo "critical fix" >> auth.js
git add . && git commit -m "fix: patch critical security vulnerability"
git switch main
git merge --no-ff hotfix/1.1.1 -m "chore: hotfix v1.1.1"
git tag -a v1.1.1 -m "Hotfix v1.1.1"
# Xem tags
git tag -l
git log --oneline --graph --all
Phần 2: Fork/Upstream Sync — Agency & Contractor Pattern
# Mô phỏng triangular workflow với 2 repos local
mkdir upstream-sim && cd upstream-sim
git init --bare -b main upstream.git
mkdir client-work && cd client-work
git clone ../upstream.git .
git config user.name "Client" && git config user.email "client@test.com"
echo "initial" > README.md
git add . && git commit -m "feat: initial commit"
git push origin main
cd ..
# "Fork" — clone upstream làm origin cho agency
mkdir agency-fork && cd agency-fork
git clone ../upstream.git .
git config user.name "Agency Dev" && git config user.email "dev@agency.com"
git remote add upstream ../upstream.git
git remote set-url --push upstream DISABLE
git remote -v
# Client thêm commits vào upstream
cd ../client-work
echo "client feature" >> README.md
git add . && git commit -m "feat: client adds new feature"
git push origin main
# Agency sync với upstream
cd ../agency-fork
git fetch upstream
git log upstream/main --oneline # thấy commit mới từ client
git switch main
git rebase upstream/main # sync
git log --oneline # history tuyến tính
# Agency tạo feature branch và làm việc
git switch -c feature/agency-work
echo "agency feature" >> README.md
git add . && git commit -m "feat: agency adds API endpoint"
# Trước khi push, sync với upstream mới nhất (giả sử client push thêm)
cd ../client-work
echo "another client commit" >> README.md
git add . && git commit -m "feat: client hotfix"
git push origin main
cd ../agency-fork
git fetch upstream
git rebase upstream/main # rebase feature branch lên upstream mới nhất
git log --oneline --graph --all
Tài liệu tham khảo
- Semantic Versioning 2.0.0
- Git Tagging
- conventional-changelog
- GitHub: Managing releases
- Git Tools: Signing Your Work
- GitHub: Fork a repo
- GitHub: Syncing a fork
- git rebase documentation
- git remote documentation
- Atlassian: Forking Workflow