git initTạo repo Git rỗng tại thư mục hiện tại. Chạy không tham số để init thư mục đang đứng.
git clone[repo]Clone repo về máy. Repo có thể nằm trên local FS hoặc remote qua HTTP/SSH.
git config user.name[name]Định nghĩa author cho commit. Thêm --global để áp dụng cho mọi repo.
git add[path]Stage thay đổi để chuẩn bị commit. Dùng . để stage tất cả, -p để stage từng hunk.
git commit -m"msg"Commit snapshot đã stage với message inline.
git statusHiển thị file đã stage / chưa stage / untracked trong working tree.
Git Cheatsheet
Tham khảo nhanh các lệnh Git theo nhóm — copy & paste khi cần.
git commit --amendThay commit gần nhất bằng staged changes hợp nhất. Không stage gì → chỉ sửa message.
git rebase[base]Rebase branch hiện tại lên base. base có thể là commit ID, branch, tag, hoặc HEAD~N.
git rebase -i HEAD~NRebase tương tác — squash, reword, drop, edit từng commit.
git reflogLịch sử mọi thay đổi HEAD — cứu hộ khi reset --hard nhầm.
git reset --hard[commit]Xoá staging + reset working tree về commit chỉ định. Nguy hiểm.
Sửa branch, commit và dọn lịch sử
git branchLiệt kê branch local. Thêm -a để xem cả remote.
git switch -c[name]Tạo và checkout sang branch mới (thay cho git checkout -b).
git switch[name]Chuyển sang branch khác.
git branch -d[name]Xoá branch local đã merge. -D để xoá force.
git push origin --delete[name]Xoá branch trên remote.
git merge[branch]Merge branch chỉ định vào branch hiện tại.
git merge --squash[branch]Gộp tất cả commit của branch thành 1 commit duy nhất.
git rebase mainĐem branch hiện tại lên đỉnh main, lịch sử thẳng hàng.
git rebase --continueTiếp tục rebase sau khi resolve conflict. --abort để huỷ.
git cherry-pick[hash]Đem 1 commit cụ thể từ branch khác về branch hiện tại.
git log --oneline --graph --allĐồ thị lịch sử commit gọn gàng cho tất cả branch.
git log -p[file]Lịch sử thay đổi của 1 file kèm diff.
git diffSo sánh working tree với staging.
git diff --stagedSo sánh staging với commit gần nhất.
git blame[file]Xem ai sửa dòng nào, commit nào.
git show[hash]Hiển thị nội dung chi tiết của một commit.
git stashCất tạm working changes để switch branch sạch.
git stash push -m"wip"Stash kèm message mô tả.
git stash listLiệt kê stash đã cất.
git stash popLấy stash gần nhất ra & xoá khỏi list.
git restore[file]Hoàn tác file (chưa stage) về trạng thái HEAD.
git restore --staged[file]Bỏ stage cho file.
git reset --soft HEAD~1Bỏ commit gần nhất, giữ thay đổi đã stage.
git reset --mixed HEAD~1Bỏ commit + unstage (mặc định của git reset).
git reset --hard HEAD~1Bỏ commit + xoá hết thay đổi. Cẩn thận!
git revert[hash]Tạo commit ngược lại để huỷ commit cũ — an toàn cho branch chia sẻ.
git clean -fdXoá file/thư mục untracked. -n để dry-run trước.
git remote -vLiệt kê remote URL.
git remote add origin[url]Thêm remote tên origin.
git fetch[remote]Tải object từ remote nhưng không merge.
git pull --rebaseFetch + rebase branch hiện tại lên upstream — lịch sử thẳng.
git push origin[branch]Đẩy branch lên remote.
git push --force-with-leaseForce push an toàn — fail nếu remote có commit mới.
git tag v1.0.0Tạo lightweight tag tại HEAD.
git tag -a v1.0.0 -m"release"Annotated tag (có metadata, recommended cho release).
git push origin v1.0.0Đẩy tag cụ thể lên remote.
git push origin --tagsĐẩy tất cả tag.
git tag -d v1.0.0Xoá tag local.
git config --global user.name"Your Name"Cấu hình tên author toàn cục.
git config --global user.email"you@x.com"Cấu hình email author.
git config --global init.defaultBranch mainĐặt branch mặc định khi git init.
git config --global pull.rebase trueMặc định pull dùng rebase thay vì merge.
git config --global core.editor"code --wait"Dùng VS Code làm editor cho commit message.