Bảng nhanh lệnh Git

Git theo dõi thay đổi tệp của bạn thành chuỗi các commit trong kho. Những lệnh này bao quát quy trình hằng ngày: tạo kho, lưu công việc, tạo nhánh, hợp nhất và chia sẻ với remote. Hãy để trang này mở khi bạn học.

Cập nhật:

LệnhTác dụng
git initCreate a new, empty Git repository in the current directory.
git cloneDownload a remote repository and its full history to your machine.
git statusShow changed, staged and untracked files in the working tree.
git addStage changes (files) to be included in the next commit.
git commitRecord staged changes as a new commit with a message.
git logDisplay the commit history of the repository.
git diffShow unstaged or staged differences between states.
git branchList, create or delete branches in the repository.
git checkout / switchSwitch branches or restore files (switch is the modern command).
git mergeJoin another branch's history into the current branch.
git rebaseReapply commits on top of another base commit, rewriting history.
git pullFetch from a remote and merge the changes into the current branch.
git pushUpload local commits to a remote repository.
git stashTemporarily shelve uncommitted changes to reapply later.
git remoteManage the set of remote repositories you track.

Ghi chú

Câu hỏi thường gặp

Sự khác biệt giữa merge và rebase là gì?
Merge kết hợp lịch sử bằng một commit mới có hai cha. Rebase phát lại các commit của bạn lên một nhánh khác, tạo lịch sử tuyến tính nhưng viết lại ID commit.
Làm sao để hoàn tác một commit?
Dùng 'git revert <commit>' để thêm một commit mới hoàn tác thay đổi (an toàn, giữ lịch sử chung). 'git reset' viết lại lịch sử và không nên dùng trên nhánh chung.
Khác biệt giữa git pull và git fetch?
Fetch tải commit và tham chiếu từ remote mà không đụng đến cây làm việc. Pull thực hiện fetch rồi merge hoặc rebase các thay đổi đó vào nhánh hiện tại của bạn.
Khi nào nên dùng git stash?
Stash tạm cất các thay đổi chưa commit để bạn đổi nhánh hoặc pull sạch sẽ, sau đó khôi phục bằng 'git stash pop'.