如何撤销 "git add --intent-to-add"?

8
当我运行命令git add --intent-to-add .时,所有未跟踪的文件状态都已从“未跟踪的文件”(git status -s显示??)更改为“未提交的更改”(git status -s现在显示A)。现在,每当我运行git diff时,我也会看到新文件。如何恢复这些状态更改,使它们重新变成“未跟踪”状态?
2个回答

10

使用git reset --mixed来完成此操作。

在执行--intent-to-add后,文件将被放置在索引中,但其内容不会被包含。您希望将其从索引中删除,而不更改工作树中的内容。这正是git reset --mixed所做的。

$ echo "some content.txt" > file.txt
$ git status ; echo "###" ; git status -s
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file.txt

nothing added to commit but untracked files present (use "git add" to track)
###
?? file.txt

# This will add the file to the index but without its content.
# Hence, for Git all the content has "changed".
$ git add --intent-to-add .

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

no changes added to commit (use "git add" and/or "git commit -a")
###
 A file.txt

$ git diff # show the content of file.txt

# Resets the index so that file.txt becomes completely new for Git.
$ git reset --mixed

$ git status ; echo "###" ; git status -s
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file.txt

nothing added to commit but untracked files present (use "git add" to track)
###
?? file.txt

$ git diff # doesn't show the content of file.txt

请注意,您可以使用-N参数来执行git reset --mixed操作,以明确地从索引中删除未跟踪的文件。

1
你是不是真正想使用 git reset --mixed - Obsidian
2
这个命令的问题在于它会将你添加的所有文件都从待提交的更改中移除。 - Nathan Gouy
@Obsidian:这篇帖子已经被Suan编辑过了,请查看历史记录。 - Rolf

1

git reset --mixed会重置索引中的所有文件(不对实际文件进行任何操作)。

如果您想将其应用于特定文件,可以使用git reset --mixed <file>,但是这已被弃用,请改用git reset -- <file>

对于从使用git add --intent-to-add添加的新文件中删除索引的特定情况,这肯定有效。它保留了文件本身的状态- git status将该文件列为未跟踪。

这有点像对已经跟踪的文件使用git restore --staged <file>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接