git restore
git
用法
git restore [--staged] [-s <commit>] <pathspec>git 2.23 引入的命令,用来还原文件内容;默认会覆盖工作区的文件,未提交的改动就没了,而 --staged 只是取消暂存。
常用选项
| 选项 | 含义 |
|---|---|
| --staged | unstage the file but keep your edits |
| --worktree | reset the file on disk; this is the default |
| --staged --worktree | both: throw the edits away and unstage |
| -s <commit> | take the content from that commit instead of the index |
| -p | choose hunk by hunk |
示例
git restore src/app.tswipes uncommitted edits to that file, with no way back
git restore --staged src/app.tstakes it out of the staging area, edits intact
git restore -s HEAD~2 src/app.tsbrings back the version from two commits ago
难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。
怎么看
- 方括号 [ ] 表示这部分可以不写。
- 省略号 … 表示可以写多个。
- 选项区分大小写——有些命令里 -r 和 -R 不是一回事。
常见问题
Q. git restore 是做什么的?
git 2.23 引入的命令,用来还原文件内容;默认会覆盖工作区的文件,未提交的改动就没了,而 --staged 只是取消暂存。
Q. 怎么写?
git restore [--staged] [-s <commit>] <pathspec> —— 方括号表示可以省略的部分。
Q. 值得记的选项有几个?
这里列了 5 个,完整列表在 man git。这条命令属于git。
相关命令
man 手册: man git-restore