git 恢复
Git 恢复
revert
是我们想要获取上一个命令时使用的命令commit
并将其添加为新的commit
,保持log
完好无损的。
第1步:找到上一个commit
:
第2步:用它来制作一个新的commit
:
让我们做一个新的commit
,我们"accidentally"删除了一个文件:
示例
git commit -m "Just a regular update, definitely no accidents here..."
[master 16a6f19] Just a regular update, definitely no accidents here...
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 img_hello_git.jpg
现在我们已经参与了我们的commit
我们想要回到的历史。让我们尝试这样做revert
。
Git 恢复在日志中查找提交
首先,我们需要找到我们想要返回的点。为此,我们需要通过log
。
为了避免很长的日志列表,我们将使用--oneline
选项,每次提交仅显示一行:
- 的前七个字符
commit hash
- 这个
commit message
所以让我们找到我们想要的点revert
:
示例
git log --oneline
52418f7 (HEAD -> master) Just a regular update, definitely no accidents here...
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from 91xjr-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch 'master' of https://github.com/91xjr-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!
我们想恢复到以前的状态commit
:52418f7 (HEAD -> master) Just a regular update, definitely no accidents here...
,我们看到它是最新的commit
。
Git 恢复 HEAD
我们恢复最新的commit
使用gitrevert HEAD
(revert
最新的变化,然后commit
),添加选项--no-edit
跳过提交消息编辑器(获取默认值revert
信息):
示例
git revert HEAD --no-edit
[master e56ba1f] Revert "Just a regular update, definitely no accidents here..."
Date: Thu Apr 22 10:50:13 2021 +0200
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 img_hello_git.jpg
现在让我们检查一下log
再次:
示例
git log --oneline
e56ba1f (HEAD -> master) Revert "Just a regular update, definitely no accidents here..."
52418f7 Just a regular update, definitely no accidents here...
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from 91xjr-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch 'master' of https://github.com/91xjr-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!
笔记:要恢复到之前的提交,请使用git revert HEAD~x
(x
是一个数字。 1 再返回 1 个,2 再返回 2 个,等等)
在下一页,我们将回顾git reset
,这将使存储库恢复到提交中的早期状态,而无需创建新的commit
。