選單
×
   ❮     
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 提供支援