简体   繁体   English

如果文件不断变化,则无法从git中提取

[英]Can't pull from git if a file is continuously changing

I've added resources/xml to my both the local and remote .gitignore files. 我已经在本地和远程.gitignore文件中添加了resources / xml。

error: Your local changes to the following files would be overwritten by merge:
resources/xml/analysis.xml

My local and remote gitignores both look like: 我的本地和远程gitignores都看起来像:

/vendor/
.env
/resources/xml/

I can't pull anything, not even stashing analysis.xml helps, because 1 sec after stash it has changed again. 我什么也拉不动,甚至无法存储analysis.xml ,因为在存储1秒钟后它又发生了变化。

UPDATE after removing from cache and deleting the conflicting file from the remote 从缓存从远程删除和删除冲突的文件之后更新

CONFLICT (modify/delete): resources/xml/analysis.xml deleted in 
d58b27586850da80e85119be6005fbe538a5e8ab and modified in HEAD. 
Version HEAD of resources/xml/analysis.xml left in tree.
Automatic merge failed; fix conflicts and then commit the result.

after this I needed another git rm resources/xml/analysis.xml and I can pull now. 之后,我需要另一个git rm resources/xml/analysis.xml ,现在就可以执行操作。 Thanks 谢谢

It seems the file is being tracked by Git, despite its entry in .gitignore . 尽管文件已在.gitignore输入,但似乎该文件已被Git跟踪。

This happens when you have added the file to Git before, only to gitignore afterwards: Git does not remove files from its index when you ignore them but only does not add them to the index. 当您之前将文件添加到Git,然后才将gitignore添加到gitignore时,就会发生这种情况:当您忽略文件时,Git不会从其索引中删除文件,而不会将它们添加到索引中。

You need to remove the file from the Git index (but not from your harddrive): 您需要从Git索引中删除文件(而不是从硬盘驱动器中删除):

git rm --cached resources/xml/analysis.xml

or 要么

git rm --cached -r resources/xml/

when there are additional files you want to remove. 当还有其他文件要删除时。

Afterwards, the file will be unknown to Git and will be ignored in the future. 之后,该文件将是Git未知的,以后将被忽略。

What's happening here is that Git is protecting you from losing changes in that file. 这里发生的是Git保护您避免丢失该文件中的更改。

You may have added the file to your .gitignore but that doesn't tell Git to stop caring about it when it tracks that file. 您可能已将文件添加到.gitignore但这并没有告诉Git在跟踪该文件时不要再关心它。 So when you have local changes, and a pull/merge would result in updating the file, Git stops so you don't lose those local changes. 因此,当您进行本地更改时,通过拉/合并将导致文件更新,Git会停止,因此您不会丢失这些本地更改。

If you want Git to really forget the file, you will have to remove it from the repository, otherwise Git will not care about what's in the .gitignore about the file. 如果您希望Git真正忘记该文件,则必须将其从存储库中删除,否则Git不会在意.gitignore有关该文件的内容。 To remove the file from the repository, without deleting the physical file, you can do this: 要从存储库中删除文件而不删除物理文件,可以执行以下操作:

git rm --cached resources/xml/analysis.xml

Then you will have to commit that change. 然后,您将不得不进行更改。 Only from then, Git will really ignore the file. 只有从那时起,Git才会真正忽略该文件。

Note that this doesn't necessarily stop Git from protecting your local file. 请注意,这不一定会阻止Git保护您的本地文件。 If you end up merging a commit in which that file still existed and in which it was changed, then Git may end up telling you again, that the file would be touched. 如果您最终合并了一个文件仍然存在且已更改的提交,那么Git可能最终再次告诉您该文件将被触摸。 So you might want to temporarily rename the file to protect your local changes in those cases. 因此,在这种情况下,您可能需要临时重命名文件以保护本地更改。

Note that it's always a good idea to check the output of git status in these situations. 请注意,在这些情况下检查git status的输出始终是一个好主意。 It will tell you in your original situation, that the file is still known to Git and that it has some uncommitted changes to it. 它会在您的原始情况下告诉您,该文件仍然是Git 已知的 ,并且它具有一些未提交的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM