git says “The following untracked working tree files would be overwritten by checkout” when switching branches

匿名 (未验证) 提交于 2019-12-03 00:46:02

问题:

I have a branch called "develop" in remote bare repo "hub" and I have a checkout copy of the develop branch in my local git repository (user poomalai). I have deleted (git rm --cached) a file (Mydirectory/myfile.php) from another checkout copy (user raj) and pushed to the hub repository.

Now when I pull the changes from the hub to "poomalai" it worked fine and I got the commit message of file deletion in the git log. I have added the file in the .gitignore file in the user poomalai. Now I have created the file in my local repo (user poomalai).

Now the file is no more in the version control and changes to the file is not tracked by git.

But when I try to switch the branch to some other, it says the following

error: The following untracked working tree files would be overwritten by checkout:     Mydirectory/myfile.php Please move or remove them before you can switch branches. Aborting 

When I remove the file using linux rm command, I am able to switch branches. If I create the file again, it again throws the same error. I need the file to be in the directory but it should not be tracked by git.

I tried following commands:

git rm ----> says fatal: pathspec 'diamonds_webservice/dbconnect.php' did not match any files  git gc git reset --hard HEAD git pull 

Nothing works. Please someone help me

回答1:

This could be possible because the the file is added to the repo in the 'target branch', ensure that the file is git rm 'ed from the 'target branch', and then try switching branches.

As charles said, git does it to ensure data integrity, so that nothing is lost when switching directories.



回答2:

It's a good thing that git warns you about this, because switching to a branch that doesn't have the file deleted will... delete it. So you're doing it right, remove (or rename) it before checking out a branch that have it deleted.

So it's just that git cares about your data before overwriting it, since it's unversioned you won't be able to recover it after the checkout.



回答3:

The only thing that worked for me:

in android studio - go to event log - click 'view them' hyperlink. - then manually hit the minus bar to delete the files in the popup window.

(because it was only 4-5 files i renamed the files first) I had to do it twice as some items was not deleted on the first try...



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!