git git在切换分支时说“以下未跟踪的工作树文件将被结帐覆盖”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6829671/
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
git says "The following untracked working tree files would be overwritten by checkout" when switching branches
提问by pooamlairaj
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
).
我develop
在远程裸仓库中hub
有一个develop
分支,我在本地 git 存储库(用户poomalai
)中有一个分支的结帐副本。
I have deleted (git rm --cached
) a file (Mydirectory/myfile.php
) from another checkout copy (user raj
), and pushed to the hub repository.
我已从另一个结帐副本(用户)中删除 ( git rm --cached
) 一个文件 ( ),并将其推送到集线器存储库。Mydirectory/myfile.php
raj
Now, when I pull the changes from the hub to (user poomalai
), it works fine, and I have the commit message file deletion
in the git log.
现在,当我将更改从集线器拉到 (user poomalai
) 时,它工作正常,并且我file deletion
在 git 日志中有提交消息。
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
).
我已将文件添加到.gitignore
(user poomalai
)中的文件中。
现在我已经在我的本地存储库(用户poomalai
)中创建了该文件。
Now the file is no longer in the version control, and changes to the file is not tracked by git.
现在该文件不再处于版本控制中,并且 git 不会跟踪对文件的更改。
But when I try to switch to some other branch 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
错误:以下未跟踪的工作树文件将被结帐覆盖:
Mydirectory/myfile.php
请在切换分支之前移动或删除它们。
中止
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.
当我使用 linuxrm
命令删除文件时,我可以切换分支。
如果我再次创建文件,它会再次引发相同的错误。
我需要该文件在目录中,但它不应该被 git 跟踪。
I tried following commands:
我尝试了以下命令:
git rm
fatal: pathspec 'diamonds_webservice/dbconnect.php' did not match any files
致命:路径规范“diamonds_webservice/dbconnect.php”与任何文件都不匹配
git gc
git reset --hard HEAD
git pull
Nothing works. Please someone help me
没有任何作用。请有人帮助我
采纳答案by Krishna Shasankar
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.
这是可能的,因为该文件已添加到“目标分支”中的存储库中。
确保文件是git rm
从“目标分支”“ed”的,然后尝试切换分支。
As @CharlesBsaid, git does it to ensure data integrity, so that nothing is lost when switching directories.
正如@CharlesB所说,git 这样做是为了确保数据完整性,以便在切换目录时不会丢失任何内容。
回答by CharlesB
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 has it deleted.
git 警告你这一点是一件好事,因为切换到一个没有删除文件的分支将......删除它。
所以你做对了:在检出删除它的分支之前删除(或重命名)它。
So it's just that git cares about your data before overwriting it.
Since it's unversioned you wouldn't be able to recover it after the checkout.
所以这只是 git 在覆盖之前关心你的数据。
由于它是未版本化的,您将无法在结帐后恢复它。
回答by Yuli Hua
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.
在 android studio - 转到事件日志 - 单击“查看它们”超链接。- 然后手动点击减号以删除弹出窗口中的文件。
(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...
(因为只有 4-5 个文件,所以我先重命名了文件)我不得不重复两次,因为第一次尝试时某些项目没有被删除......