Ngày 30: Chia sẻ một số mẹo thao tác Git hữu ích trong công việc
Sau 29 ngày học Git, bài viết cuối cùng này chia sẻ các mẹo và thủ thuật thực tế giúp thao tác Git hàng ngày hiệu quả và thú vị hơn.
Mẹo cấu hình
Cấu hình toàn cục (Global Configuration)
# Set default editor
git config --global core.editor "code --wait"
# Enable color UI
git config --global color.ui auto
# Set default branch name
git config --global init.defaultBranch main
# Auto-correct typos
git config --global help.autocorrect 1
# Cache credentials
git config --global credential.helper cache --timeout=3600
Các Alias hữu ích
# Short status
git config --global alias.st status
# Short log
git config --global alias.ll "log --oneline --graph --all --decorate"
# Last commit
git config --global alias.last "log -1 HEAD"
# Undo last commit (keep changes)
git config --global alias.undo "reset HEAD~1 --mixed"
# Amend without editing message
git config --global alias.amend "commit --amend --no-edit"
# List branches sorted by last modified
git config --global alias.br "branch --sort=-committerdate"
# Show changed files in last commit
git config --global alias.dl "diff --name-only HEAD~1"
# Search commit messages
git config --global alias.search "log --all --grep"
# Nuke branch (force delete)
git config --global alias.nuke "branch -D"
Mẹo tăng năng suất
Điều hướng nhanh
# Jump to repository root
alias gr='cd $(git rev-parse --show-toplevel)'
# Open repo in browser
alias browse='git remote get-url origin | xargs open'
Tìm kiếm
# Find file in history
git log --all --full-history -- "**/filename.*"
# Search in all commits
git log -S "search term" --source --all
# Find who changed a line
git blame filename
# Prettier blame
git blame -w -C -C -L 10,20 filename
Dọn dẹp
# Delete merged branches
git branch --merged main | grep -v "^*\|main" | xargs git branch -d
# Delete all local branches except main
git branch | grep -v "main" | xargs git branch -D
# Remove untracked files (dry run first!)
git clean -n
git clean -fd
# Prune remote branches
git fetch --prune
Các lệnh tiết kiệm thời gian
Commit nhanh
# Add and commit in one step
git commit -am "Quick fix"
# Add, commit, and push
alias gcp='git commit -am "$1" && git push'
Quản lý Branch
# Create and checkout branch in one step
git checkout -b feature/new-feature
# Switch to previous branch
git checkout -
# Show branches with last commit
git branch -v
# Show remote tracking branches
git branch -vv
Thủ thuật Stash
# Stash with message
git stash push -m "Work in progress on feature X"
# List stashes with details
git stash list --pretty=format:"%C(yellow)%gd%C(reset) - %C(bold)%s%C(reset)"
# Apply and drop in one step
git stash pop
# Apply specific stash
git stash apply stash@{2}
# Stash including untracked files
git stash -u
Các kỹ thuật nâng cao
Cherry-Pick nhiều Commit
# Cherry-pick a range of commits
git cherry-pick A^..B
# Cherry-pick from another branch
git cherry-pick feature-branch~3..feature-branch
Staging tương tác
# Stage parts of a file
git add -p filename
# Interactive add
git add -i
Mẹo Diff
# Word diff instead of line diff
git diff --word-diff
# Ignore whitespace changes
git diff -w
# Show diff of staged changes
git diff --staged
# Diff between branches
git diff main..feature-branch
# Diff specific file between commits
git diff commit1 commit2 -- filename
Định dạng Log
# Pretty log
git log --pretty=format:"%C(yellow)%h%C(reset) - %C(green)(%cr)%C(reset) %s %C(blue)<%an>%C(reset)"
# Log with graph
git log --graph --abbrev-commit --decorate --all
# Show files changed per commit
git log --stat
# Show patches
git log -p
Khắc phục sự cố
Hoàn tác lỗi
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Undo all uncommitted changes
git checkout .
# Unstage file
git reset HEAD filename
# Restore file from HEAD
git checkout HEAD -- filename
Khôi phục
# Find lost commits
git reflog
# Recover deleted branch
git branch branch-name <commit-hash>
# Find dangling commits
git fsck --lost-found
Gỡ lỗi (Debug)
# See who introduced a bug (binary search)
git bisect start
git bisect bad # Current version is bad
git bisect good <commit> # Known good commit
# Git checks out middle commit
# Test and mark as good or bad
git bisect good/bad
# Repeat until found
git bisect reset
# Blame with line range
git blame -L 10,20 filename
# Show commit that introduced line
git log -S "function name" --source --all
Tối ưu hóa quy trình làm việc
Pre-Commit Hook
Tạo .git/hooks/pre-commit:
#!/bin/bash
# Run tests before commit
npm test
if [ $? -ne 0 ]; then
echo "Tests failed, commit aborted"
exit 1
fi
Mẫu thông điệp Commit
Tạo ~/.git-commit-template:
# <type>: <subject>
#
# <body>
#
# <footer>
#
# Types: feat, fix, docs, style, refactor, test, chore
Thiết lập làm mặc định:
git config --global commit.template ~/.git-commit-template
Mẹo tích hợp
Git với VS Code
# Open current repo in VS Code
code .
# Open merge tool
git mergetool --tool=vscode
Git với Vim
# Set Vim as default editor
git config --global core.editor vim
# Vim as diff tool
git config --global diff.tool vimdiff
git config --global difftool.prompt false
Mẹo hiệu năng
Repository lớn
# Shallow clone (faster)
git clone --depth 1 https://github.com/user/repo.git
# Partial clone (Git 2.22+)
git clone --filter=blob:none https://github.com/user/repo.git
# Sparse checkout (specific folders)
git sparse-checkout init
git sparse-checkout set folder1 folder2
Status nhanh hơn
# Use untracked cache
git config core.untrackedCache true
# Use file system monitor
git config core.fsmonitor true
Mẹo bảo mật
Ký GPG
# Generate GPG key
gpg --gen-key
# List keys
gpg --list-secret-keys --keyid-format LONG
# Configure Git
git config --global user.signingkey <key-id>
git config --global commit.gpgsign true
# Sign commits
git commit -S -m "Signed commit"
Thông tin xác thực (Credentials)
# Never store passwords in config
# Use credential helpers instead
# SSH for authentication (recommended)
ssh-keygen -t ed25519 -C "email@example.com"
# Use HTTPS with token
# Generate personal access token on GitHub
# Use it as password
Lời khuyên cuối cùng
Danh sách kiểm tra các thực hành tốt nhất
- Commit thường xuyên, push đều đặn
- Viết thông điệp commit có ý nghĩa
- Giữ các commit nguyên tử (atomic) và tập trung
- Rẽ nhánh cho mỗi tính năng/bản sửa lỗi
- Tự review mã của mình trước khi tạo PR
- Pull trước khi push
- Không bao giờ force push lên các branch dùng chung
- Sử dụng .gitignore đúng cách
- Tag các bản phát hành
- Tài liệu hóa quy trình làm việc của bạn
Khi mọi thứ gặp sự cố
- Đừng hoảng: Git hiếm khi làm mất dữ liệu
- Kiểm tra reflog: Đó là lưới an toàn của bạn
- Tạo bản sao lưu: Rẽ nhánh trước khi thử nghiệm
- Nhờ giúp đỡ: Cộng đồng Git rất sẵn lòng giúp đỡ
- Học từ sai lầm: Mỗi lỗi dạy bạn một điều gì đó
Chúc mừng!
Bạn đã hoàn thành 30 ngày làm chủ Git! Bạn đã học:
- Nền tảng và kiến trúc của Git
- Các chiến lược rẽ nhánh và merge
- Thao tác lịch sử (reset, rebase, amend)
- Quy trình làm việc với remote và cộng tác
- Quy trình làm việc trên GitHub và các thực hành tốt nhất
- Các mẹo nâng cao và khắc phục sự cố
Tiếp tục học hỏi
- Thực hành thường xuyên
- Đóng góp cho mã nguồn mở
- Học từ lịch sử Git của người khác
- Khám phá các tính năng nâng cao
- Luôn cập nhật với các bản phát hành Git
Lời kết
Git là một công cụ mạnh mẽ, càng sử dụng càng có giá trị. Đừng cố nhớ mọi thứ - hãy đánh dấu hướng dẫn này và tham khảo lại khi cần. Điều quan trọng nhất là tiếp tục thực hành và đừng ngại thử nghiệm (trong một môi trường an toàn).
Chúc bạn code vui vẻ, và mong mọi lần merge của bạn luôn không có xung đột!
Liên kết tham khảo
Cảm ơn bạn!
Cảm ơn bạn đã đồng hành trong hành trình Git 30 ngày này. Hãy tiếp tục thực hành, tiếp tục học hỏi, và nhớ rằng - mọi chuyên gia Git cũng từng là người mới bắt đầu!