未追踪和已更改的文件是相同的。

3

如果某个文件没有被跟踪,Git如何告诉我它已经发生了变化?当它被发现为未跟踪时,它又如何被删除呢?

$ git status
On branch updated-code-foundation
Your branch is ahead of 'origin/updated-code-foundation' by 16 commits.
  (use "git push" to publish your local commits)
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    component/portal/src/Joppli/Acl/AclInterface.php
    deleted:    container/php-apache/config/apache/default.conf
    deleted:    container/php-apache/config/php/php.ini

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

    component/portal/src/Joppli/Acl/AclInterface.php
    container/php-apache/config/

我通过执行 git add . (无需提交)来解决这个问题。
  1. 为什么会出现这种情况?
  2. 我该如何避免这种情况的发生?
2个回答

3

1. 场景如下:

  • 您有一个文件被跟踪并且之前已经提交到仓库中
  • 您使用git rm命令删除了该文件
  • 该文件再次被创建

示例:

git add myfile
git commit -m "myfile"
git rm myfile
echo test > myfile
git status

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    myfile

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

    myfile

2. 我不确定如何防止这种情况发生,因为这取决于文件的具体情况。可能是这些文件是自动生成的,并且有人错误地提交了它们,然后试图将其删除。

也许它们应该被放在.gitignore中?


可能是那个做了一些鬼畜操作的IDE :) - superhero

1
如果您添加了“hunks”,也可能出现这种情况。
使用-p标志添加文件并拆分更改。
您将在工作目录中(如果以前未添加,则未跟踪)和暂存区中都有它,并带有刚刚添加的hunks。
正如您在图像中看到的,您可以拆分更改并仅将其中几个添加到暂存区。

enter image description here


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