Setup and Config
Getting and Creating Projects
Basic Snapshotting
Branching and Merging
Sharing and Updating Projects
Inspection and Comparison
Patching
Debugging
External Systems
Server Admin
Guides
- gitattributes
- Command-line interface conventions
- Everyday Git
- Frequently Asked Questions (FAQ)
- Glossary
- Hooks
- gitignore
- gitmodules
- Revisions
- Submodules
- Tutorial
- Workflows
- All guides...
Administration
Plumbing Commands
-
2.49.0
2025-03-14
- 2.43.1 → 2.48.1 no changes
-
2.43.0
2023-11-20
- 2.35.1 → 2.42.4 no changes
-
2.35.0
2022-01-24
- 2.30.1 → 2.34.8 no changes
-
2.30.0
2020-12-27
- 2.27.1 → 2.29.3 no changes
-
2.27.0
2020-06-01
- 2.25.1 → 2.26.3 no changes
-
2.25.0
2020-01-13
- 2.23.1 → 2.24.4 no changes
-
2.23.0
2019-08-16
概述
git restore [<options>] [--source=<tree>] [--staged] [--worktree] [--] <pathspec>… git restore [<options>] [--source=<tree>] [--staged] [--worktree] --pathspec-from-file=<file> [--pathspec-file-nul] git restore (-p|--patch) [<options>] [--source=<tree>] [--staged] [--worktree] [--] [<pathspec>…]
描述
用还原源的一些内容还原工作树中的指定路径。如果一个路径被跟踪,但在还原源中不存在,它将被删除以匹配源。
该命令还可以用来用`--staged`恢复索引中的内容,或者用`--staged --worktree`同时恢复工作树和索引。
默认情况下,如果给了`--staged`,内容将从`HEAD`恢复,否则从索引恢复。使用`--source`来恢复不同的提交。
关于这三个命令的区别,见git[1]中的 "重置、恢复和还原"。
这个命令是试验性的。其行为可能会改变。
选项
-
-s <tree>
-
--source=<tree>
-
用给定树的内容恢复工作树的文件。通常是通过命名与之相关的提交、分支或标签来指定源树。
如果没有指定,如果给了`--staged`,则从`HEAD`恢复内容,否则从索引恢复。
As a special case, you may use
"<rev-A>...<rev-B>"
as a shortcut for the merge base of <rev-A> and <rev-B> if there is exactly one merge base. You can leave out at most one of <rev-A>_ and <rev-B>, in which case it defaults toHEAD
. -
-p
-
--patch
-
Interactively select hunks in the difference between the restore source and the restore location. See the "Interactive Mode" section of git-add[1] to learn how to operate the
--patch
mode.请注意,`--patch`可以不接受任何pathspec,并会提示恢复所有修改的路径。
-
-W
-
--worktree
-
-S
-
--staged
-
指定恢复的位置。如果两个选项都没有指定,默认情况下会还原工作树。指定`--staged`将只恢复索引。指定两个选项将同时还原。
-
-q
-
--quiet
-
安静,抑制反馈信息。意味着
--no-progress
。 -
--progress
-
--no-progress
-
当它附加到终端时,除非指定
--quiet
,否则默认情况下会在标准错误流中报告进度状态。这个标志可以启用进度报告,即使没有附在到终端,而不管--quiet
。 -
--ours
-
--theirs
-
When restoring files in the working tree from the index, use stage #2 (
ours
) or #3 (theirs
) for unmerged paths. This option cannot be used when checking out paths from a tree-ish (i.e. with the--source
option).Note that during
git rebase
andgit pull --rebase
,ours
andtheirs
may appear swapped. See the explanation of the same options in git-checkout[1] for details. -
-m
-
--merge
-
从索引中恢复工作目录树上的文件时,在未合并路径中重新创建冲突合并。 该选项不能用于从工作目录树中检出路径(即使用
--source
选项)。 -
--conflict=<style>
-
The same as
--merge
option above, but changes the way the conflicting hunks are presented, overriding themerge.conflictStyle
configuration variable. Possible values aremerge
(default),diff3
, andzdiff3
. -
--ignore-unmerged
-
当从索引中恢复工作目录树上的文件时,如果有未合并的条目,并且没有指定
--ours
、--theirs
、--merge
或--conflict
,不要中止操作。工作目录树上未合并的路径将被忽略。 -
--ignore-skip-worktree-bits
-
In sparse checkout mode, the default is to only update entries matched by <pathspec> and sparse patterns in
$GIT_DIR/info/sparse-checkout
. This option ignores the sparse patterns and unconditionally restores any files in <pathspec>. -
--recurse-submodules
-
--no-recurse-submodules
-
If <pathspec> names an active submodule and the restore location includes the working tree, the submodule will only be updated if this option is given, in which case its working tree will be restored to the commit recorded in the superproject, and any local modifications overwritten. If nothing (or
--no-recurse-submodules
) is used, submodules working trees will not be updated. Just like git-checkout[1], this will detachHEAD
of the submodule. -
--overlay
-
--no-overlay
-
In overlay mode, never remove files when restoring. In no-overlay mode, remove tracked files that do not appear in the <tree> of
--source=<tree>
, to make them match <tree> exactly. The default is no-overlay mode. -
--pathspec-from-file=<file>
-
Pathspec is passed in <file> instead of commandline args. If <file> is exactly
-
then standard input is used. Pathspec elements are separated by LF or CR/LF. Pathspec elements can be quoted as explained for the configuration variablecore.quotePath
(see git-config[1]). See also--pathspec-file-nul
and global--literal-pathspecs
. -
--pathspec-file-nul
-
只有在使用
--pathspec-from-file
选项时才有意义。指定路径元素用 NUL 字符分隔,所有其他字符都按字面意思(包括换行符和引号)表示。 -
--
-
不将之后的参数解释为选项。
-
<路径规范>...
-
限制受操作影响的路径。
更多细节请参见 gitglossary[7] 中的 路径规范 条目。
实例
The following sequence switches to the master
branch, reverts the Makefile
to two revisions back, deletes hello.c
by mistake, and gets it back from the index.
$ git switch master $ git restore --source master~2 Makefile (1) $ rm -f hello.c $ git restore hello.c (2)
-
将一个文件从另一个提交中取出
-
从索引中恢复`hello.c`
如果你想恢复_所有的C源文件,使之与索引中的版本一致,你可以说
$ git restore '*.c'
注意 "*.c "周围的引号。 文件`hello.c`也将被恢复,尽管它已经不在工作树中了,因为文件globbing是用来匹配索引中的条目的(不是由shell在工作树中)。
要恢复当前目录下的所有文件
$ git restore .
或者用 top 路径规范恢复所有工作目录树文件(见gitglossary[7])
$ git restore :/
将索引中的文件恢复到与 HEAD
中的版本一致(这与使用git-reset[1]相同)
$ git restore --staged hello.c
或者你可以同时恢复索引和工作目录树(这与使用 git-checkout[1] 相同)
$ git restore --source=HEAD --staged --worktree hello.c
或更实用但可读性较差的简短形式:
$ git restore -s@ -SW hello.c
GIT
属于 git[1] 文档