Git 指令速查
Git 將檔案變更記錄為倉庫裡的一系列提交。這些指令覆蓋日常流程:建倉、儲存改動、分支、合併以及與遠端共享。學習時把本表放在手邊。
| 指令 | 作用 |
|---|---|
| git init | Create a new, empty Git repository in the current directory. |
| git clone | Download a remote repository and its full history to your machine. |
| git status | Show changed, staged and untracked files in the working tree. |
| git add | Stage changes (files) to be included in the next commit. |
| git commit | Record staged changes as a new commit with a message. |
| git log | Display the commit history of the repository. |
| git diff | Show unstaged or staged differences between states. |
| git branch | List, create or delete branches in the repository. |
| git checkout / switch | Switch branches or restore files (switch is the modern command). |
| git merge | Join another branch's history into the current branch. |
| git rebase | Reapply commits on top of another base commit, rewriting history. |
| git pull | Fetch from a remote and merge the changes into the current branch. |
| git push | Upload local commits to a remote repository. |
| git stash | Temporarily shelve uncommitted changes to reapply later. |
| git remote | Manage the set of remote repositories you track. |
說明
- 多數指令支援參數(如 -m 寫資訊、-a 全部);執行 'git help <指令>' 看完整列表。
- 分支名很輕量、可隨意丟棄——常在功能分支上提交,再合併回主分支。
- 在執行 rebase、reset 等破壞性指令前,先確保工作已提交或暫存。
常見問題
- merge 和 rebase 有什麼差別?
- merge 用一個有兩個父節點的新提交合併歷史。rebase 把你的提交重放到另一分支之上,得到線性歷史,但會改寫提交的 ID。
- 如何撤銷一次提交?
- 用 'git revert <提交>' 新增一個反向提交來撤銷改動(安全,保留共享歷史)。'git reset' 會改寫歷史,不應在共享分支上使用。
- git pull 和 git fetch 有何不同?
- fetch 從遠端下載提交與參照,但不動你的工作區。pull 先 fetch,再把改動合併或變基到目前分支。
- 什麼時候用 git stash?
- stash 臨時擱置未提交的改動,方便切換分支或乾淨地拉取程式碼,之後用 'git stash pop' 取回。