Git 指令速查

Git 將檔案變更記錄為倉庫裡的一系列提交。這些指令覆蓋日常流程:建倉、儲存改動、分支、合併以及與遠端共享。學習時把本表放在手邊。

更新日期:

指令作用
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.

說明

常見問題

merge 和 rebase 有什麼差別?
merge 用一個有兩個父節點的新提交合併歷史。rebase 把你的提交重放到另一分支之上,得到線性歷史,但會改寫提交的 ID。
如何撤銷一次提交?
用 'git revert <提交>' 新增一個反向提交來撤銷改動(安全,保留共享歷史)。'git reset' 會改寫歷史,不應在共享分支上使用。
git pull 和 git fetch 有何不同?
fetch 從遠端下載提交與參照,但不動你的工作區。pull 先 fetch,再把改動合併或變基到目前分支。
什麼時候用 git stash?
stash 臨時擱置未提交的改動,方便切換分支或乾淨地拉取程式碼,之後用 'git stash pop' 取回。