在工作文件夹中修改文件后,为什么Git会切换分支? [英] Why does Git switch my branches when I have modified files in the working folder?

查看:107
本文介绍了在工作文件夹中修改文件后,为什么Git会切换分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个分支.我在一个分支上更改了一些文件,但没有添加或提交它们.所做的更改仅在我的工作文件夹中.更改是在跟踪的文件上,但未添加或提交,更改仅在工作文件夹中.

I have two branches. I changed some files while on one branch but did not add or commit them. The changes are in my working folder only. The changes are on tracked files but not added or committed, the changes are only in the working folder.

现在我签出了另一个分支,它成功了.我在另一个分支上,对文件所做的更改仍在工作文件夹中.

Now I checkedout the other branch and it worked. I'm on the other branch and the changes I made to the files are still in the working folder.

我不明白原因.我本来希望Git拒绝切换分支,并告诉我我尚未提交更改,所以我应该提交或隐藏它们或对它们执行某些操作.

I don't understand the reason for this. I would have expected Git to refuse to switch branch and tell me that I have uncommited changes, that I should commit or stash or do something with them.

我希望结帐会重写我的工作文件夹,但是我的更改仍然存在.

I expected the checkout to rewrite my working folder, but my changes are still there.

那是为什么?无论我在哪个分支上,为什么我的更改都存在?为什么Git不能告诉我工作文件夹中存在修改过的文件并拒绝切换分支?

Why is that? Why are my changes present no matter on what branch I am? Why desn't Git tell me of the presence of modified files in my working folder and refuse to switch branch?

推荐答案

分支指向提交.在分支上进行提交之前,更改"不属于分支.

Branches point to commits. A "change" isn't part of a branch until you make a commit on that branch.

您的工作树和索引未直接附加到分支,因此,如果您开始进行本地编辑,然后确定您确实要在其他分支上进行此更改,则可以签出该分支并继续工作.

Your working tree and index are not directly attached to a branch, so if you start doing some locally editing and then decide that you really wanted to make this change on a different branch you can check out that branch and carry on working.

只有当您进行本地修改(暂存或不暂存)时,Git才会拒绝更改分支,这些修改是基于要在其间切换的分支与要切换到的分支之间不同的文件的.

Git will refuse to change branches only if you have local modifications (staged or unstaged) which are based on files that are different between the branch that you are switching from and the branch that you are switching to.

这篇关于在工作文件夹中修改文件后,为什么Git会切换分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆