簡體   English   中英

git rm cached 和 git reset HEAD 的區別

[英]Difference between git rm cached and git reset HEAD

我猜我對git rm --cached感到困惑。
我有一個存儲庫和一個提交的文件。 我修改了文件,然后執行: git add myfile
該文件現已上演。
當我做git status

# On branch master   
# Changes to be committed:  
#   (use "git reset HEAD <file>..." to unstage)  
#  
#       modified:   com/main/StringMain.java  
#  

現在該文件是一個修改后的跟蹤文件。 所以我假設那是在集結區。 所以我無法理解建議的含義是什么(use "git reset HEAD <file>..." to unstage) 所以我做了: git rm --cached而后跟一個git commit 但這似乎使我的文件不再被跟蹤並使其不被跟蹤。
如果我做git status

# On branch master  
# Untracked files:  
#   (use "git add <file>..." to include in what will be committed)  
#  
#       com/  
nothing added to commit but untracked files present (use "git add" to track)  

那么發生了什么?

這是一種思考方式:

git rm --cached [file]

這只是從跟蹤中刪除文件(添加文件后的狀態 - 即git add [file]

git reset HEAD [file]

這只是繼續跟蹤對文件的更改,但會將其放回“未暫存”區域。

這是一個例子。

首先,我創建了 4 個未跟蹤的文件:

$ for i in {A..D}; do touch $i; echo "First line" > $i; done
$ ls
A   B   C   D
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    A
    B
    C
    D

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

接下來,我使用 git add 跟蹤它們,然后我將提交更改:

$ git add .

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   A
    new file:   B
    new file:   C
    new file:   D

$ git commit -m "First Commit"
[master 6e8d625] First Commit
 4 files changed, 4 insertions(+)
 create mode 100644 A
 create mode 100644 B
 create mode 100644 C
 create mode 100644 D

$ git status
On branch master
nothing to commit, working directory clean

現在我將修改文件 A 以便 git 接收更改,然后我將更改的文件添加到暫存區:

$ echo "First line of file A" > A

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   A

no changes added to commit (use "git add" and/or "git commit -a")

$ git add A

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   A

現在這就是差異的重要性。

我給你的第一個例子是當你使用git rm --cached時會發生什么:

$ git rm --cached A
rm 'A'

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    A

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

    A

注意文件 A 現在是如何未被跟蹤的,很像它在開始時將它添加到 git 之前的情況(當使用“ git add . ”時)。

現在,第二個例子是如果我要使用git reset HEAD來代替:

$ git reset HEAD A
Unstaged changes after reset:
M   A

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   A

no changes added to commit (use "git add" and/or "git commit -a")

在這里,您會注意到它將文件 A 的狀態重置回未暫存狀態,但仍會繼續跟蹤它的更改。

git rm --cached從索引中刪除文件。

git reset HEAD將文件的索引版本重置為其在HEAD提交中的狀態。

所以區別在於第一個刪除文件,而第二個將其恢復到最后提交的版本。


為了驗證這一點,您可以使用git diff將工作樹與索引進行比較,並使用git diff --cached將索引與頭部提交進行比較。

當您運行git rm --cached ,修改后的文件將從索引中完全刪除。 它仍然存在於工作目錄和最后一次提交中。 如果將索引與最后一次提交進行比較:

git diff --cached modified_file

您將看到修改后的文件不存在於索引中。 這是通過以下方式證實的:

git status

這將顯示文件被安排在提交時刪除。 您的工作目錄不受git rm --cached影響,因為--cached直接在索引中工作。

假設您從暫存區(git rm 文件)中刪除了一個文件,因此它會從您的工作目錄和暫存區中刪除該文件,現在讓我們再次將其刪除,但在暫存之后,因此您將其暫存但不在您的暫存區中工作目錄,如果你使用 git rm --cached file ,你會把你的文件放回到你的工作目錄(而且 git 不再跟蹤它,它不會告訴你暫存它),但是如果你使用 git reset HEAD 文件,文件已從舞台區域中刪除,而無需返回您的工作目錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM