git - git rm cached 和 git reset HEAD 之间的区别

标签 git version-control

我对 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 - git rm cached 和 git reset HEAD 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17122480/

相关文章:

git - 在 Azure DevOps 中发布的 Git 分支未在 VS2013 或 Git 命令行中显示

带有 LFS 的 Git bundle

git - 获取自上一个标记以来的所有 git 提交

git - 使用 git-flow 的多个开发分支

version-control - 带有共享二进制文件的 TFS 分支

Xcode + Perforce : it frequently shows me the spinning wheel for no reason! 我能做什么?

php - 是否有支持 http 的好 php git 客户端?

git - 通过子模块显示 Git 中的外部代码依赖性

Jenkins 的 git 插件无法从本地计算机克隆存储库。错误代码 128

svn - Mercurial 存档特定文件夹/文件?