Git 列出了已修改但未暂存以进行提交的相同文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10676173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:56:57  来源:igfitidea点击:

Git lists same file modified and not staged for commit?

git

提问by user1406626

For some reason Git is telling me I have a file which is both "to be committed" and also "not staged for commit"? This doesn't make sense:

出于某种原因,Git 告诉我我有一个既“待提交”又“未提交提交”的文件?这没有意义:

% git status 
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   Dir1/Dir2/filename.cpp
#
# 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:   Dir1/Dir2/filename.cpp

Dir1, Dir2and filename.cppare definitely all the same file. I had to add filename.cppback again to get it to appear as a change to be committed (after it was originally in the repository). About the only thing that may have caused the problem was that I stashed, pull --rebase, then popped the stash? .gitignoredoes not list Dir1, Dir2or filename.cppanywhere and the patterns don't suggest they would catch this file?

Dir1,Dir2并且filename.cpp肯定都是同一个文件。我不得不filename.cpp再次添加以使其显示为要提交的更改(在它最初在存储库中之后)。关于可能导致问题的唯一原因是我藏匿了pull --rebase,然后弹出stash? .gitignore不列出Dir1Dir2filename.cpp任何地方,并且模式不表明他们会捕获此文件?

回答by Cameron Skinner

It means that you made a change to filename.cpp, added that change (with git add), then made another change that has not yet been added.

这意味着您对 进行了更改filename.cpp,添加了该更改(使用git add),然后进行了另一项尚未添加的更改。

The "changes to be committed" part means that Git has updated its index with a change. When you run git commit, the changes to the index will be used to create the new commit object.

“要提交的更改”部分意味着 Git 已通过更改更新了其索引。运行时git commit,对索引的更改将用于创建新的提交对象。

The "changes not staged" part shows the difference between the index and your working copy.

“未暂存的更改”部分显示了索引和您的工作副本之间的差异。

You can reproduce what you're seeing like so:

您可以像这样重现您所看到的内容:

  • Edit filename.cpp
  • Run git status. You'll see "changes not staged".
  • Run git add filename.cpp
  • Run git status. You'll see "changes to be committed".
  • Edit filename.cpp again
  • Run git status. You'll see both "changes not staged" and "changes to be committed".
  • 编辑文件名.cpp
  • 运行git status。您会看到“未上演的更改”。
  • git add filename.cpp
  • 运行git status。您将看到“要提交的更改”。
  • 再次编辑 filename.cpp
  • 运行git status。您将看到“未暂存的更改”和“要提交的更改”。

Does that make sense? It's always a little tricky explaining how Git works.

那有意义吗?解释 Git 的工作原理总是有点棘手。

回答by ams

In CVS, SVN, BZR, and no doubt a dozen other tools, once a file is added that means it'll be included in the next commit.

在 CVS、SVN、BZR 以及毫无疑问的十几种其他工具中,一旦添加了文件,就意味着它将包含在下一次提交中。

Not so GIT.

不是那么 GIT。

In git you don't "add a file", you "add a change" (sometimes it's called "stage a change").

在 git 中,您不是“添加文件”,而是“添加更改”(有时称为“暂存更改”)。

If you change a file twice you have to "add" it twice.

如果您更改文件两次,则必须“添加”两次。

回答by Matthew Farwell

If you modify a file, add it and them remodify the file, you'll get this behaviour. When you do a git add, it adds the changes up to that point, and commiting after that without re-adding only commits the first set of added changes.

如果你修改一个文件,添加它,然后他们重新修改文件,你会得到这种行为。当您执行 git add 时,它会将更改添加到该点,然后在不重新添加的情况下提交仅提交第一组添加的更改。

$ git status
# On branch master
nothing to commit (working directory clean)

matthewfarwell (master)
$ vi foo.txt <-- add lines 1,2,3 here

matthewfarwell (master)
$ git add foo.txt

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

matthewfarwell (master)
$ vi foo.txt <-- add lines 4,5,6, here

matthewfarwell (master)
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   foo.txt
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   foo.txt
#

So if I commit now, only the lines 1,2,3 will be commited.

因此,如果我现在提交,则只会提交第 1、2、3 行。