0

[Java Backend Zero to Hello] BÀI 3.1: GIT CƠ BẢN

Java Backend Zero to Hello

📚 Bài viết thuộc series Java Backend Zero to Hello 📌 Phần: Phase 3: Công Cụ & Build Tools | Bài 29/86


BÀI 3.1: GIT CƠ BẢN

Mục tiêu

  • Hiểu Git và tại sao cần Git
  • Sử dụng các lệnh Git cơ bản
  • Quản lý branch và merge
  • Làm việc với remote repository

1. GIT LÀ GÌ?

Git là hệ thống quản lý phiên bản phân tán (Distributed Version Control System) phổ biến nhất hiện nay.

Lợi ích

  • Theo dõi lịch sử thay đổi code
  • Làm việc nhóm hiệu quả
  • Branching/merging dễ dàng
  • Backup code an toàn
  • Open source trên GitHub/GitLab

2. CÀI ĐẶT

# Windows - tải từ https://git-scm.com/
# macOS
brew install git

# Linux
sudo apt install git

Cấu hình ban đầu

git config --global user.name "Tên của bạn"
git config --global user.email "email@example.com"
git config --global init.defaultBranch main
git config --global core.editor "code --wait"  # VS Code

3. CÁC KHÁI NIỆM

3.1 Repository (Repo)

Nơi chứa toàn bộ code và lịch sử thay đổi.

3.2 Working Directory

Thư mục làm việc hiện tại.

3.3 Staging Area

Vùng chứa tạm, chuẩn bị commit.

3.4 Commit

Snapshot của code tại một thời điểm.

3.5 Branch

Nhánh phát triển độc lập.

3.6 Remote

Repository trên server (GitHub, GitLab).

Vòng đời file

Untracked → Staged → Committed
     ↓         ↓
   Modified  (sau khi commit)

4. CÁC LỆNH CƠ BẢN

4.1 Khởi tạo repo

git init                    # Tạo repo mới
git clone <url>             # Clone repo có sẵn

4.2 Kiểm tra trạng thái

git status                  # Trạng thái files
git log                     # Lịch sử commit
git log --oneline           # Lịch sử ngắn gọn
git log --graph --oneline   # Xem graph

4.3 Thêm và commit

git add <file>              # Thêm 1 file
git add .                   # Thêm tất cả
git add *.java              # Thêm theo pattern

git commit -m "Message"     # Commit với message
git commit -am "Message"    # Add + commit (chỉ file đã tracked)

4.4 Xem thay đổi

git diff                    # So sánh working dir vs staging
git diff --staged           # So sánh staging vs last commit
git diff <branch1> <branch2>  # So sánh 2 branch

4.5 Undo

git restore <file>          # Hủy thay đổi ở working dir
git restore --staged <file> # Unstage
git reset HEAD~1            # Hủy commit cuối (giữ thay đổi)
git reset --hard HEAD~1     # Hủy commit cuối (xóa thay đổi)

5. BRANCH

5.1 Tạo và chuyển branch

git branch                  # Liệt kê branch
git branch <name>           # Tạo branch mới
git checkout <name>         # Chuyển branch
git checkout -b <name>      # Tạo + chuyển
git switch <name>           # Chuyển (Git 2.23+)
git switch -c <name>        # Tạo + chuyển

5.2 Merge

git merge <branch>          # Merge branch vào current
git merge --no-ff <branch>  # Merge với commit (giữ lịch sử)

5.3 Xóa branch

git branch -d <name>        # Xóa branch đã merge
git branch -D <name>        # Xóa branch (force)

6. REMOTE

6.1 Quản lý remote

git remote -v                       # Xem remote
git remote add origin <url>         # Thêm remote
git remote remove origin            # Xóa remote

6.2 Push và Pull

git push origin <branch>            # Push lên remote
git push -u origin main             # Push + set upstream
git pull                            # Pull + merge
git pull --rebase                   # Pull + rebase
git fetch                           # Chỉ tải về, không merge

6.3 Clone

git clone <url>                     # Clone mặc định
git clone -b <branch> <url>         # Clone branch cụ thể
git clone --depth 1 <url>           # Shallow clone

7. .GITIGNORE

File .gitignore liệt kê các file/folder không muốn track.

# IDE
.idea/
*.iml
.vscode/
.project
.classpath

# Build
target/
build/
*.class
*.jar

# OS
.DS_Store
Thumbs.db

# Logs
*.log
logs/

# Env
.env
.env.local

8. GITHUB CƠ BẢN

8.1 Tạo repo trên GitHub

  1. Vào github.com → New repository
  2. Đặt tên, chọn Public/Private
  3. Không tạo README (nếu đã có local)

8.2 Kết nối local với GitHub

git remote add origin https://github.com/username/repo.git
git push -u origin main

8.3 SSH Key (khuyến nghị)

# Tạo key
ssh-keygen -t ed25519 -C "email@example.com"

# Copy public key
cat ~/.ssh/id_ed25519.pub

# Thêm vào GitHub: Settings → SSH and GPG keys → New SSH key

9. BÀI TẬP THỰC HÀNH

Bài 1: Tạo project đầu tiên

mkdir my-project
cd my-project
git init
echo "# My Project" > README.md
git add .
git commit -m "Initial commit"

Bài 2: Branch workflow

# Tạo branch feature
git checkout -b feature/login

# Làm việc trên branch
echo "Login code" > login.java
git add .
git commit -m "Add login feature"

# Merge về main
git checkout main
git merge feature/login

Bài 3: Push lên GitHub

git remote add origin https://github.com/user/repo.git
git push -u origin main

10. TÓM TẮT

Lệnh Mô tả
git init Tạo repo
git clone Sao chép repo
git add Thêm vào staging
git commit Lưu snapshot
git status Xem trạng thái
git log Lịch sử
git diff So sánh
git branch Quản lý branch
git checkout/switch Chuyển branch
git merge Hợp nhất
git push Đẩy lên remote
git pull Kéo về local

Bài tiếp theo: 3.2 Git nâng cao


🧭 Điều Hướng Series

⬅️ Bài trước: PHASE 3: CÔNG CỤ & BUILD TOOLS - Tổng Quan & Mục Tiêu

📋 Lộ trình tổng quan: Xem Toàn Bộ Series

➡️ Bài tiếp theo: BÀI 3.2: GIT NÂNG CAO


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí