Skip to main content
  1. Learn center
  2. Software Development
  3. Guides
  4. Git tutorial
  5. Cách sử dụng Git
  6. Cách quản lý lịch sử trong Gi
  7. Xóa một cam kết trước đó
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

Xóa một cam kết trước đó

Đi đến thư mục git-tutorial/tutorial3 mà bạn đã tải xuống trước đó.

Khi bạn kiểm tra lịch sử của kho lưu trữ này, nó sẽ giống như sau:

Current history

Chúng tôi sẽ hoàn tác hai cam kết trước đó bằng cách sử dụng lệnh git reset.

Trước tiên, hãy mở tệp sample.txt và xác minh rằng nội dung của tệp giống như sau:

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

Sử dụng lệnh reset để xóa hai cam kết trước đó, như bên dưới.

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

Tệp sample.txt sẽ không còn chứa hai dòng cuối cùng (tức là “commit: Save the status of an index” và “pull: Obtain the content of the remote repository”).

Xác minh rằng những cam kết này không còn trong lịch sử bằng lệnh 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 trỏ đến cam kết ban đầu trước khi quá trình đặt lại diễn ra. Điều này có ích khi bạn vô tình đặt lại.

Bạn có thể khôi phục lịch sử trước đó bằng cách thực hiện đặt lại về 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