選單
×
   ❮     
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 Revert


Git Revert

revert 是我們用來將之前的 commit 新增為新的 commit,同時保持 log 不變。

步驟 1:查詢之前的 commit

Git Revert Step 1

步驟 2:使用它來建立一個新的 commit

Git Revert Step 2

讓我們建立一個新的 commit,其中我們“意外”刪除了一個檔案

示例

git commit -m "Just a regular update, definitely no accidents here..."
[master 16a6f19] Just a regular update, definitely no accidents here...
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 img_hello_git.jpg

現在我們的 commit 歷史中有一部分是我們想要回退的。讓我們嘗試使用 revert 來做到這一點。


Git Revert 在 Log 中查詢 Commit

首先,我們需要找到我們想要返回的點。為此,我們需要瀏覽 log

為了避免非常長的日誌列表,我們將使用 --oneline 選項,它每行顯示一個提交,包含

  • commit hash 的前七個字元
  • commit message

所以,讓我們找到我們想要 revert 的點

示例

git log --oneline
52418f7 (HEAD -> master) 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: 52418f7 (HEAD -> master) Just a regular update, definitely no accidents here...,我們看到這是最新的 commit



Git Revert HEAD

我們使用 git revert HEADrevert 最新的更改,然後 commit)來回退最新的 commit,並新增 --no-edit 選項來跳過提交訊息編輯器(獲取預設的 revert 訊息)

示例

git revert HEAD --no-edit
[master e56ba1f] Revert "Just a regular update, definitely no accidents here..."
 Date: Thu Apr 22 10:50:13 2021 +0200
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 img_hello_git.jpg

現在讓我們再次檢查 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!

注意: 要回退到更早的提交,請使用 git revert HEAD~xx 是一個數字。1 表示回退一個,2 表示回退兩個,以此類推)

在下一頁,我們將介紹 git reset,它會將儲存庫恢復到之前的提交狀態,而不會建立新的 commit


透過練習來測試自己

練習

顯示儲存庫的 log,每行只顯示一個 commit

git  

開始練習


×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

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