Cấp độ: elementary25 phútminMiễn phíFreeYoung CreatorYoung Creator

Git: Quản lý phiên bản codeGit: Version control your code

Repo Git với 5 commit lịch sửA Git repo with 5 commits

Bạn sẽ học gìWhat you will learn

  • Khởi tạo repo với git initInitialize a repo with git init
  • Stage và commit với git add/commitStage and commit with git add/commit
  • Xem lịch sử với git logView history with git log

Bạn sẽ tạo gìWhat you will build

Repo Git với 5 commit lịch sửA Git repo with 5 commits

Nội dung bài họcLesson content

Git là gì? Tại sao cần?What is Git? Why do you need it?

Git là hệ thống quản lý phiên bản (version control). Nó lưu lịch sử mọi thay đổi của code, giúp bạn quay lại phiên bản cũ nếu sai. Git cũng giúp nhiều người làm việc cùng nhau trên 1 dự án. GitHub là dịch vụ lưu Git repo online.Git is a version control system. It saves the history of every code change, letting you go back to an old version if you make a mistake. Git also helps multiple people work together on 1 project. GitHub is a service that stores Git repos online.

Khởi tạo repo và commit đầu tiênInitialize a repo and first commit

git init tạo repo mới trong thư mục hiện tại. git add . đưa tất cả file vào "staging area" (chờ commit). git commit -m "mô tả" lưu snapshot. Mỗi commit cần thông điệp rõ ràng: "feat: thêm trang chủ" hoặc "fix: sửa lỗi đăng nhập".git init creates a new repo in the current directory. git add . puts all files into the "staging area" (waiting to commit). git commit -m "description" saves a snapshot. Each commit needs a clear message: "feat: add homepage" or "fix: fix login bug".

bash
                      
                        # Tạo repo mới
git init

# Tạo file và stage
echo "print('Hello')" > hello.py
git add .

# Commit đầu tiên
git commit -m "feat: thêm file hello.py"

# Xem lịch sử
git log --oneline
# Output: a1b2c3d feat: thêm file hello.py
                      
                    

git status và git loggit status and git log

git status cho biết: file nào đã thay đổi, file nào đã staged, file nào chưa tracked. git log hiển thị lịch sử commit: mã hash, tác giả, ngày, thông điệp. Dùng git log --oneline để xem gọn 1 dòng mỗi commit.git status tells you: which files changed, which are staged, which are untracked. git log shows commit history: hash code, author, date, message. Use git log --oneline for a compact 1-line-per-commit view.

bash
                      
                        # Kiểm tra trạng thái
git status
# Changes to be committed:
#   new file:   hello.py

# Xem lịch sử gọn
git log --oneline
# a1b2c3d feat: thêm file hello.py
# e4f5g6h init: khởi tạo repo

# Xem chi tiết 1 commit
git show a1b2c3d
                      
                    

Quy ước commit messageCommit message conventions

Tiền tố phổ biến: feat (tính năng mới), fix (sửa lỗi), docs (tài liệu), style (định dạng), refactor (tái cấu trúc), test (test case), chore (bảo trì). Ví dụ: "feat: thêm trang liên hệ", "fix: sửa lỗi hiển thị giá". Thông điệp ngắn, rõ, mô tả "what" và "why".Common prefixes: feat (new feature), fix (bug fix), docs (documentation), style (formatting), refactor (restructuring), test (test cases), chore (maintenance). Example: "feat: add contact page", "fix: fix price display bug". Messages should be short, clear, describe "what" and "why".

Bài tập thực hànhHands-on exercises

1Tạo repo Git mới, thêm 1 file Python, commit với thông điệp "feat: bài học Python đầu tiên".Create a new Git repo, add 1 Python file, commit with message "feat: first Python lesson".

Gợi ýHint

git init → tạo file → git add . → git commit -m "..."git init → create file → git add . → git commit -m "..."

Lời giảiSolution
bash
                      
                        git init
echo "print('Hello World')" > bai1.py
git add .
git commit -m "feat: bài học Python đầu tiên"
git log --oneline
                      
                    

2Sửa file bai1.py, thêm comment, commit lại với "docs: thêm comment giải thích".Edit bai1.py, add a comment, commit again with "docs: add explanatory comment".

Gợi ýHint

Sửa file → git add . → git commit -m "docs: ..."Edit file → git add . → git commit -m "docs: ..."

Lời giảiSolution
bash
                      
                        # Sửa file bai1.py thêm comment
# print("Hello World")  # In lời chào
git add .
git commit -m "docs: thêm comment giải thích"
git log --oneline
                      
                    

Kiểm traQuiz

Chọn đáp án đúng cho mỗi câu. Nhấn để xem giải thích.Choose the correct answer for each question. Click to see explanation.

Câu 1: Git dùng để làm gì?Q1: What is Git used for?

Câu 2: Lệnh nào đưa file vào staging area?Q2: Which command puts files into the staging area?

Câu 3: Tiền tố "fix:" trong commit message nghĩa là gì?Q3: What does the "fix:" prefix in a commit message mean?

Câu 4: Lệnh nào xem lịch sử commit?Q4: Which command shows commit history?

Code mẫuSample code

bash
                # Các lệnh Git cơ bản / Basic Git commands
git init                         # Tạo repo mới
git add .                        # Stage tất cả file
git commit -m "feat: first page" # Tạo commit
git log --oneline                # Xem lịch sử
git status                       # Kiểm tra trạng thái