首页·终端命令

git fetch

git

用法

git fetch [--all] [--prune] [<remote> [<branch>]]

只把远端的新提交取下来,不动你的文件和分支,要生效还得 merge 或 rebase;服务器上已删的分支在 --prune 之前还会留着。

常用选项

选项含义
--allfrom every remote
--prunedelete tracking refs whose branch is gone on the server
--tagsalso fetch tags
--unshallowfill in the history a --depth clone left out
--depth <n>deepen a shallow clone by n commits
-fupdate a ref even when it is not a fast-forward

示例

git fetch --all --prune

refreshes every remote and clears dead branches

git fetch origin main

updates origin/main without touching your files

git fetch --unshallow

turns 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