Skip to main content
  1. Learn center
  2. Software Development
  3. Guides
  4. Git tutorial
  5. 如何使用 Git
  6. 如何在 Git 中管理歷史紀錄
  7. 移除以前的提交
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

移除以前的提交

請前往您之前下载的git-tutorial/tutorial3目錄。

當您檢查此儲存庫的歷史紀錄時,它將如下所示:

Current history

我們將使用 git reset 指令來撤銷前兩次提交。

首先,開啟sample.txt檔案,並確認其內容如下所示:

Anyone can learn Git with this tutorial and Backlog
add: Register a change in an index
commit: Save the status of an index
pull: Obtain the content of the remote repository

使用重設指令來刪除前兩次提交,如下所示。

Delete the commit
$ git reset --hard HEAD~~
  HEAD is now at 326fc9f append description of the add command

sample.txt檔案將不再包含最後兩行 (即commit: Save the status of an indexpull: Obtain the content of the remote repository)。

使用 git log 指令來驗證這些提交是否已經不在歷史紀錄中。

$ git log
  commit 326fc9f70d022afdd31b0072dbbae003783d77ed
  Author: yourname <yourname@yourmail.com>
  Date:   Mon Jul 16 23:17:56 2022 +0900

      append description of the add command

  commit 48eec1ddf73a7fb508ef664efd6b3d873631742f
  Author: yourname <yourname@yourmail.com>
  Date:   Mon Jul 16 23:16:14 2022 +0900

      first commit

ORIG_HEAD 指向重設發生前的原始提交。當您不小心發出重置時,這會派上用場。

您可以透過執行對 ORIG_HEAD 的重設來恢復以前的歷史紀錄。

$ git reset --hard ORIG_HEAD
  HEAD is now at 0d4a808 append description of the pull command

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life