git checkout
git
用法
git checkout <branch> | git checkout -b <new> | git checkout -- <file>既能切分支又能還原檔案的舊命令,在 git 2.23 裡拆成了 switch 和 restore;checkout -- <file> 會把未提交的改動徹底刪掉。
常用選項
| 選項 | 含義 |
|---|---|
| -b <new> | create the branch and switch to it |
| -B <new> | create or reset it to the current commit |
| -- <file> | overwrite the file with the staged version, losing your edits |
| --track <remote>/<branch> | start a local branch that follows a remote one |
| --detach | sit on a commit with no branch attached |
| -f | throw away local changes to switch anyway |
範例
git checkout -b feature/loginbranches off the current commit and moves there
git checkout mainswitches branch, same as git switch main
git checkout -- src/app.tsdiscards uncommitted edits to that file; they are gone
難的地方在於復原有好幾種:把分支的指向挪走,和補一個反向提交,不是一回事。
怎麼看
- 方括號 [ ] 表示這部分可以不寫。
- 省略號 … 表示可以寫多個。
- 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。
常見問題
Q. git checkout 是做什麼的?
既能切分支又能還原檔案的舊命令,在 git 2.23 裡拆成了 switch 和 restore;checkout -- <file> 會把未提交的改動徹底刪掉。
Q. 怎麼寫?
git checkout <branch> | git checkout -b <new> | git checkout -- <file> —— 方括號表示可以省略的部分。
Q. 值得記的選項有幾個?
這裡列了 6 個,完整列表在 man git。這條命令屬於git。
相關命令
man 手冊: man git-checkout