菜单
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Git Reset


Git Reset

reset 是我们想要将仓库回退到之前的 commit,并丢弃该 commit 之后所做的任何更改时使用的命令。

步骤 1:查找之前的 commit

Git Reset Step 1

步骤 2:将仓库回退到该步骤

Git Reset Step 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!

通过练习来测试自己

练习

commit 重置到哈希为 abc1234 的 commit

git  

开始练习


×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持