GIT - Fail to checkout to other branch when nothing to commit - when file is in gitignore

我们两清 提交于 2019-12-11 16:47:28

问题


Trying to checkout to another branch

I have a .gitignore file with

.someFile

trying to checkout to another branch fails even when i dont have any changes

dev@cool:~/proj/source$ git status
# On branch master
nothing to commit, working directory clean
dev@cool:~/proj/source$ git checkout someBranchName 
error: Your local changes to the following files would be overwritten by checkout:
    .someFile
Please, commit your changes or stash them before you can switch branches.
Aborting
dev@cool:~/proj/source$ 

removing the file from .gitignore and add it again didnt help

running command :

      git update-index --assume-unchanged .someFile

or

 git rm --cached .someFile

didnt help

any idea ?


回答1:


What happens if you temporarily remove your .gitignore file? It sounds like Git is tracking the file, even though you're telling it to ignore it.

You might try:

git rm --cached .someFile

To delete the file from git (without deleting it from your workspace). Then commit.




回答2:


Switching between branches should be treated as applying patch that represents the difference between them.

In your case the difference that git tries to apply is "add new file .somefile, but only in case that it doesn't exist.".

Then it runs into conflict.

If you want to overwrite file with the version from target branch then you can simply delete it in work tree before switch.

If you want to preserve ignored file then you can just rename it to put aside (this would not cause any conflict until its new temporary name doesn't match with some other file name). Then make a switch. After that just replace ".somefile" with the one that you temporary renamed.
This would cause work tree change to file if content will not match.



来源:https://stackoverflow.com/questions/22204376/git-fail-to-checkout-to-other-branch-when-nothing-to-commit-when-file-is-in

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