Git Reset
Git Reset
reset
是我们想要将仓库回退到之前的 commit
,并丢弃该 commit
之后所做的任何更改时使用的命令。
步骤 1:查找之前的 commit
步骤 2:将仓库回退到该步骤
在上一个章节之后,我们的 commit
历史中有一个我们可以回退的点。让我们尝试使用 reset
来实现这一点。
Git Reset 在日志中查找 Commit
首先,我们需要找到我们要返回的点。为此,我们需要查看 log
。
为了避免很长的 log
列表,我们将使用 --oneline
选项,该选项每行只显示一个 commit
,其中包含:
- The first seven characters of the
commit hash
- this is what we need to refer to in our reset command. (commit 哈希的前七个字符 - 这是我们在 reset 命令中需要引用的内容。) - The
commit message
(commit 消息)
所以,让我们找到我们要 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
,这是我们在开始胡乱修改之前最后的一个 commit。
Git Reset
我们使用 git reset commithash
(其中 commithash
是我们在 log
中找到的 commit 哈希的前 7 个字符) 来将仓库重置回指定的 commit。
示例
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
历史可能会很危险。通常情况下,对您自己的本地仓库进行此类更改是可以的。但是,您应该避免对 remote
仓库进行重写历史的操作,特别是当其他人也在使用它们时。
Git Undo Reset
即使 commit 不再显示在 log
中,它也并未从 Git 中移除。
如果您知道 commit 哈希,您可以 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!