1. Learn center
  2. Software Development
  3. Guides
  4. Git tutorial
  5. 如何使用 Git
  6. 如何在 Git 中管理歷史紀錄
  7. 撤消提交
Git

Project and code management together.

Try it free

撤消提交

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

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

Current history

我們將使用 git revert 指令,來撤銷append description of the pull command的提交。

首先,開啟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

我們將使用 revert 指令,來撤銷名為append description of the pull command的最新 HEAD 提交,如下所示:

$ git revert HEAD
[main d47bb1d] Revert "append description of the pull command"
  1 files changed, 1 insertions(+), 2 deletions(-)

現在,當我們開啟sample.txt時,您會看到最後一行 (即pull: Obtain the content of the remote repository) 將不再存在。

The history of repository after the revert

使用 git log 指令來驗證新的歷史紀錄是否如下所示:

$ git log
  commit 7bcf5e3b6fc47e875ec226ce2b13a53df73cf626
  Author: yourname <yourname@yourmail.com>
  Date:   Wed Jul 18 15:46:28 2022 +0900

      Revert "append description of the pull command"

      This reverts commit 0d4a808c26908cd5fe4b6294a00150342d1a58be.

  commit 0d4a808c26908cd5fe4b6294a00150342d1a58be
  Author: yourname <yourname@yourmail.com>
  Date:   Mon Jul 16 23:19:26 2022 +0900

      append description of the pull command

  commit 9a54fd4dd22dbe22dd966581bc78e83f16cee1d7
  Author: yourname <yourname@yourmail.com>
  Date:   Mon Jul 16 23:19:01 2022 +0900

      append description of the commit command

  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

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life