git fetch
git
用法
git fetch [--all] [--prune] [<remote> [<branch>]]只把远端的新提交取下来,不动你的文件和分支,要生效还得 merge 或 rebase;服务器上已删的分支在 --prune 之前还会留着。
常用选项
| 选项 | 含义 |
|---|---|
| --all | from every remote |
| --prune | delete tracking refs whose branch is gone on the server |
| --tags | also fetch tags |
| --unshallow | fill in the history a --depth clone left out |
| --depth <n> | deepen a shallow clone by n commits |
| -f | update a ref even when it is not a fast-forward |
示例
git fetch --all --prunerefreshes every remote and clears dead branches
git fetch origin mainupdates origin/main without touching your files
git fetch --unshallowturns a shallow clone into a full one
难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。
怎么看
- 方括号 [ ] 表示这部分可以不写。
- 省略号 … 表示可以写多个。
- 选项区分大小写——有些命令里 -r 和 -R 不是一回事。
常见问题
Q. git fetch 是做什么的?
只把远端的新提交取下来,不动你的文件和分支,要生效还得 merge 或 rebase;服务器上已删的分支在 --prune 之前还会留着。
Q. 怎么写?
git fetch [--all] [--prune] [<remote> [<branch>]] —— 方括号表示可以省略的部分。
Q. 值得记的选项有几个?
这里列了 6 个,完整列表在 man git。这条命令属于git。
相关命令
man 手册: man git-fetch