Skip to content

Git Restore

一句话解释

git restore 丢弃工作树中未提交变更或从特定提交恢复文件,为此取代 git checkout。

用大白话说

git restore 是现代(Git 2.23+)撤销工作树变更的命令。它为此特定任务取代了 overloaded 的 git checkout,使意图更清晰。你用它丢弃未提交编辑、恢复已删文件,或把另一提交的文件内容复制到工作树。两种常见形式:git restore <file> 丢弃文件未提交变更(恢复为与暂存区一致)。git restore --staged <file> 取消暂存(从索引移除但保留工作树变更),取代 git reset HEAD <file>。git restore --source=HEAD~1 <file> 从更早提交恢复文件。restore 只动工作树(或加 --staged 动索引)——绝不移动当前分支或 HEAD。这使它比旧的 checkout 重载更安全、更可预测。配合 git switch(换分支)实现现代按意图拆分的工作流。

示例

bash
# Discard uncommitted changes to a file
echo "oops" >> app.js
git restore app.js       # app.js back to staged version

# Restore a file you deleted (but haven't committed the deletion)
rm config.json
git restore config.json  # file is back

# Unstage a file (keep the changes in working tree)
git add app.js
git restore --staged app.js

# Restore a file from 2 commits ago
git restore --source=HEAD~2 app.js

restore 丢弃工作树变更。--staged 取消暂存。--source=COMMIT 从该提交恢复。绝不移动 HEAD 或分支。

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表