HOWTO · Git

Git 刪除未提交的更改

撤消未提交的更改

本頁內容

本文將指導你如何撤消我們對本地倉庫所做的未提交更改。

使用功能時,我們可能首先建立新檔案,向現有檔案新增更改,然後刪除一些檔案。最終,我們意識到這一切都錯了,需要回到之前的提交。我們應該做什麼?

$ echo 'Add new implementation' > feature.txt
$ echo 'Enhance exising feature' >> file.txt
$ git add file.txt
$ rm deprecated_feature.txt
$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   file.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	deleted:    deprecated_feature.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature.txt

我們有幾種方法來完成它。

使用 git checkout 刪除 Git 中未提交的更改

此命令將恢復跟蹤檔案的未提交更改。被跟蹤的檔案是 git 知道的檔案,通常是在被 git add 新增之後

$ git checkout .
Updated 2 paths from the index
$ git status
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature.txt

nothing added to commit but untracked files present (use "git add" to track)

請注意,如果檔案已經通過 git add 新增到暫存區,git checkout 將不起作用。

$ echo 'Enhance exising feature' >> file.txt
$ git add file.txt
$ git checkout file.txt
Updated 0 paths from the index
$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   file.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature.txt

在上面的示例中,對 file.txt 的更改不會恢復,因為該檔案位於暫存區域中。

使用 git reset 刪除 Git 中未提交的更改

要刪除暫存區中未提交的更改,我們需要採取以下步驟。

  1. 使用 git reset 從暫存區取消暫存檔案。
  2. 使用 git checkout 撤消更改。
$ git reset file.txt
Unstaged changes after reset:
M	file.txt
$ git checkout file.txt
Updated 1 path from the index
$ git status
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature.txt

nothing added to commit but untracked files present (use "git add" to track)

使用 git reset 刪除未提交更改的另一種方法是使用選項 --hard 和引數 HEAD

$ git reset --hard HEAD
HEAD is now at 1e087f5 Make some change to file.txt
$ git status
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature.txt

nothing added to commit but untracked files present (use "git add" to track)
  • --hard 選項指定 Git 丟擲當前狀態和最後一個引數中的提交之間的所有更改。出於這個原因,這個命令被認為是危險的,應該在你執行 git status 檢查工作檔案後使用。
  • 最新提交的 HEAD 別名。

使用 git stashgit stash 刪除 Git 中未提交的更改

git checkoutgit reset 的缺點是它們無法刪除未跟蹤的檔案。feature.txt 在執行這些命令後仍然存在。

考慮第一個例子。

$ echo 'Add new implementation' > feature.txt
$ echo 'Enhance exising feature' >> file.txt
$ git add file.txt
$ rm deprecated_feature.txt
$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   file.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	deleted:    deprecated_feature.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature.txt

刪除所有未提交的更改,包括暫存檔案、已跟蹤但未暫存和未跟蹤檔案。我們將使用巧妙的方法 git stash

git stash 允許我們儲存更改,但不需要 git commit;它充當未提交檔案的臨時儲存。

在向臨時儲存新增更改後,我們將告訴 Git 刪除這些儲存。因此,所有未提交的更改都將消失。

$ git add .
$ git stash
Saved working directory and index state WIP on main: 16b9767 deprecated_feature.txt
$ git stash drop
Dropped refs/stash@{0} (aebeb2cbdcec917331f5793ef1238f5a525d29ec)
$ git status
On branch main
nothing to commit, working tree clean

總之,我們有幾種方法可以刪除未提交的更改:

  1. git checkout 僅在檔案不在暫存區時有用。
  2. git reset 對暫存區中的更改很有用,但不能刪除未跟蹤檔案的更改,需要與 git checkout 結合使用。
  3. git reset --hard HEAD 可能比上面的短,但有潛在的危險。
  4. git stashgit add . 可以刪除所有內容,包括未跟蹤的檔案。