Git checkout fails with because local changes … without local changes?

假如想象 提交于 2019-12-12 11:32:54

问题


Using Git 2.7.4 I made some changes to a branch. Commit and push to the branch.

[user:~/terraform] mybranch ± git status
On branch DIC-98-rhel-lb0
nothing to commit, working directory clean
[user:~/terraform] mybranch ±


[user:~/terraform] mybranch ± git push origin mybranch
Everything up-to-date

When I try to switch to master

[user:~/terraform] mybranch ± git checkout master
error: Your local changes to the following files would be overwritten by checkout:
    azure-openshift/.gitignore
    azure-openshift/bastion.tf
    azure-openshift/bootstrap.sh
    azure-openshift/bootstrap.tfvars
    azure-openshift/cns.tf
    azure-openshift/infra.tf
    azure-openshift/master.tf
    azure-openshift/openshift.auto.tfvars
    azure-openshift/openshift.variables.tf
    azure-openshift/revproxy.tf
Please, commit your changes or stash them before you can switch branches.
Aborting
[user:~/terraform] mybranch 1 ± 

I can also do git reset first, with the same result.

What am I missing?


回答1:


Most likely those files are ignored in your current branch (mybranch), but they are part of the master branch (perhaps they were accidentally committed at some point?). So while your current branch is clean, checking out master would overwrite those files and risk losing that data.

If you know that it's safe to overwrite those files, then you can tell git that you're quite sure you want to check out master with the -f flag:

git checkout -f master

If you're not sure, then you can try rebasing or merging master into your issue branch and working through any conflicts, etc.


For example, let's say you accidentally included a file named foo.temp, which is a log file generated when you run builds, in your master branch.

You then create a new branch, remove_foo, and add foo.temp to your .gitignore. You build (thus modifying foo.temp), commit and push your changes to the server.

Everything looks good. git status returns clean.

You then attempt to checkout master and get an error: foo.temp will be overwritten with the version in master. Git is trying to protect your work.

Fortunately, you know that this is a temporary issue, so you use git checkout -f master because you know that it's okay if foo.temp is overwritten.

Eventually, remove_foo will be peer-reviewed and merged into master and this little annoyance will go away.


If you attempted to remove some files which were accidentally included in the repo, then you may find this helpful:

Remove a file from a Git repository without deleting it from the local filesystem



来源:https://stackoverflow.com/questions/51446801/git-checkout-fails-with-because-local-changes-without-local-changes

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