Bỏ qua đến nội dung chính

Đồng bộ dự án code

Sync code projects ≠ sync như Documents. Có nhiều files không nên sync (node_modules, build artifacts, secrets) vì:

  • Dependencies (per-machine binaries) — install lại bằng npm install nhanh hơn sync
  • Build outputs — rebuild được, sync waste bandwidth
  • Secrets — .env.local per-machine
PC Laptop
┌──────────────────────────┐ ┌──────────────────────────┐
│ ~/projects/ │ ←──── sync ──→│ ~/projects/ │
│ ├─ project-a/ │ (source) │ ├─ project-a/ │
│ │ ├─ src/ ✅ │ │ │ ├─ src/ ✅ │
│ │ ├─ package.json ✅ │ │ │ ├─ package.json ✅ │
│ │ ├─ pnpm-lock.yaml ✅ │ │ │ ├─ pnpm-lock.yaml ✅ │
│ │ └─ node_modules/ ❌ │ │ │ └─ node_modules/ ❌ │
│ │ (installed local) │ │ │ (installed local)│
│ └─ project-b/ │ │ └─ project-b/ │
└──────────────────────────┘ └──────────────────────────┘
After sync: cd project-a && npm install (one-time, both machines)
# Build artifacts
**/node_modules
**/dist
**/build
**/.next
**/out
**/coverage
**/.turbo
# Dependencies caches
**/.venv
**/venv
**/__pycache__
**/*.pyc
**/.pytest_cache
# Per-machine secrets
**/.env.local
**/.env.development.local
# Database / data
**/data
**/postgres-data
**/postgres_data
**/*.sqlite
**/*.db
# Docker volumes (per-machine)
**/.docker
# IDE / OS
**/.vscode
**/.idea
**/.DS_Store
**/Thumbs.db
**/desktop.ini
# Logs + temp
**/*.log
**/logs
**/tmp
**/.tmp
**/.cache
Terminal window
cd ~/projects
for proj in project-a project-b; do
echo "=== Installing $proj ==="
cd ~/projects/$proj && npm install && cd ~/projects
done

Câu hỏi phổ biến: “Sync .git/ folder thì git có break không?”

Trả lời: KHÔNG, sync .git/ thường safe. .git/ chứa:

  • objects/ — immutable content-addressed (hash = unique)
  • refs/ — pointer files, append-only nature
  • HEAD, index — current state

Edge case có thể conflict:

  • 2 máy commit cùng branch lúc 1 máy offline → 2 different refs/heads/<branch> content
  • Mitigation: pull/push qua git remote là source of truth, không rely 100% Syncthing

Best practice:

  1. Commit + push lên remote (GitHub/GitLab) trước khi switch máy
  2. Switch máy → mở project → check git status clean
  3. Pull mới từ remote: git pull — pickup changes
  4. Tiếp tục work

→ Syncthing sync working dir + uncommitted changes. Git remote = canonical history.

Pros:

  • Preserve uncommitted commits
  • Working dir + git state in-sync
  • Branch state consistent

Cons:

  • Tốn space (vd opencode .git/ có thể >1GB)
  • Per-machine objects/pack rebuild khi git gc

→ Em recommend sync .git/ trừ khi space rất tight.

  1. Trước khi switch máy: commit work-in-progress (vd git commit -am "WIP") — không bắt buộc nhưng best practice

  2. Đóng IDE — release file locks

  3. Đợi 30s — Syncthing flush

  4. Mở máy kia → IDE → continue work

  5. Pull remote nếu có changes từ teammates: git pull

Bonus: đồng bộ dự án local (không có git remote)

Phần tiêu đề “Bonus: đồng bộ dự án local (không có git remote)”

Cho projects không có GitHub/GitLab remote (vd ~/projects/n8n/ — local Docker stack):

  • Sync via Syncthing là backup duy nhất
  • ⚠ Nếu cả 2 máy fail → mất source. Fix: weekly tar archive vào shared folder hoặc cloud
  • Hoặc tạo private GitHub repo:
Terminal window
cd ~/projects/n8n
gh repo create AI-Workforce-Solutions/n8n-private --private --source=. --push