问题描述
使用Git 2.7.4,我对分支进行了一些更改.提交并推送到分支.
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
当我尝试切换到主版本
[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 ±
我也可以先执行git reset
,结果相同.
I can also do git reset
first, with the same result.
我想念什么?
推荐答案
这些文件很可能在您当前的分支(mybranch
)中被忽略,但是它们是master
分支的一部分(可能是它们被意外提交)某点?).因此,当您当前的分支干净时,签出master将会覆盖那些文件,并有丢失该数据的风险.
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.
如果您知道覆盖这些文件是安全的,则可以告诉git,您肯定要使用-f
标志检出master
:
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
如果不确定,则可以尝试将master重新建立基础或合并到您的问题分支中,并解决任何冲突等.
If you're not sure, then you can try rebasing or merging master into your issue branch and working through any conflicts, etc.
例如,假设您在master分支中意外包含了一个名为foo.temp
的文件,该文件是在运行build时生成的日志文件.
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.
然后创建一个新分支remove_foo
,并将foo.temp
添加到.gitignore
中.您进行构建(从而修改foo.temp
),提交更改并将其推送到服务器.
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.
一切看起来都不错. git status
返回干净.
Everything looks good. git status
returns clean.
然后您尝试检出master并出现错误:foo.temp
将被master
中的版本覆盖. Git试图保护您的工作.
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.
幸运的是,您知道这是一个临时性问题,因此您使用git checkout -f master
是因为您知道如果覆盖foo.temp
没关系.
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.
最终,remove_foo
将被同行评审并合并为master,而这种烦恼将消失.
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:
这篇关于Git检出失败,因为本地更改...没有本地更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!