简体   繁体   中英

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.

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:

/vendor/
.env
/resources/xml/

I can't pull anything, not even stashing analysis.xml helps, because 1 sec after stash it has changed again.

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. Thanks

It seems the file is being tracked by Git, despite its entry in .gitignore .

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.

You need to remove the file from the Git index (but not from your harddrive):

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.

What's happening here is that Git is protecting you from losing changes in that file.

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. 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.

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. 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.

Note that this doesn't necessarily stop Git from protecting your local file. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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