Git 重置
Git 重置
reset
是我们想要将仓库回退到之前的 commit
时使用的命令,它会丢弃该 commit
之后所做的任何更改。
步骤 1:查找之前的 commit
步骤 2:将仓库回退到该步骤
在上一章中,我们在 commit
历史记录中有一部分可以回退。让我们尝试使用 reset
来做到这一点。
Git 重置在日志中查找提交
首先,我们需要找到我们想要回退到的点。为此,我们需要查看 log
。
为了避免非常长的 log
列表,我们将使用 --oneline
选项,它只显示每个 commit
的一行,显示
commit hash
的前七个字符 - 这是我们在重置命令中需要引用的内容。commit message
所以让我们找到我们想要 reset
的点
示例
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 w3schools-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/w3schools-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
:9a9add8 (origin/master) Added .gitignore
,这是我们在开始修改内容之前最后一个提交。
Git 重置
我们使用 git reset commithash
将仓库重置回指定的提交(commithash
是我们在 log
中找到的提交哈希的前 7 个字符)。
示例
git reset 9a9add8
现在让我们再次检查 log
示例
git log --oneline
9a9add8 (HEAD -> master, origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-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/w3schools-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
历史记录可能很危险。通常,对您自己的本地仓库进行此类更改是可以的。但是,您应该避免进行重写历史记录的更改到 远程
仓库,尤其是在其他人也在使用它们的情况下。
Git 撤销重置
即使提交不再显示在 log
中,它也不会从 Git 中删除。
如果您知道提交哈希,您可以 reset
到它
示例
git reset e56ba1f
现在让我们再次检查 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 w3schools-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/w3schools-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!