git 重置


git重置

reset是当我们想要将存储库移回到以前的版本时使用的命令commit,放弃此后所做的任何更改commit

第1步:找到上一个commit:

Git Reset Step 1

步骤 2:将存储库移回该步骤:

Git Reset Step 2

上一章之后,我们有一部分commit我们可以回顾的历史。让我们尝试这样做reset


Git Reset 在日志中查找提交

首先,我们需要找到我们想要返回的点。为此,我们需要通过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 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:9a9add8 (origin/master) Added .gitignore,我们开始搞乱之前的最后一个。



git重置

我们reset我们的存储库返回到特定提交使用git reset commithashcommithash是我们在中找到的提交哈希的前 7 个字符log):

示例

git reset 9a9add8

现在让我们检查一下log再次:

示例

git log --oneline
9a9add8 (HEAD -> master, 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存储库的历史记录可能很危险。通常可以对您自己的本地存储库进行此类更改。但是,您应该避免进行重写历史记录的更改remote存储库,特别是当其他人正在使用它们时。


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 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!

通过练习测试一下

练习:

resetcommit哈希值 abc1234:

git  

开始练习