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!
GitHub
Bitbucket
GitLab