Git:分支机构应该隔离更改,还是不应该? [英] Git: branches should isolate the changes, or should not?

查看:48
本文介绍了Git:分支机构应该隔离更改,还是不应该?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我已经对Git有了一个不错的掌握,但就在现在,我感到很惊讶.我以为分支机构会将变更与其他分支机构隔离开,所以我在一个新分支机构中进行了大型的实验性重构.这种重构意味着移动许多文件.当我切换到母版时,我仍然可以看到更改!

I thought I had already a decent grasp of Git, but just now I've been surprised. I thought that branches would isolate the changes from other branches, so I started a big experimental refactoring in a new branch. This refactoring, implied moving many files. When I switched to master I could still see the changes though!

我去了沙盒存储库以复制问题:

I went to my sandbox repository to replicate the problem:

$ git branch crazy-refactoring
$ git checkout crazy-refactoring
$ rm README
$ git checkout master
$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    README
#
no changes added to commit (use "git add" and/or "git commit -a")
$ ls README
ls: cannot access README: No such file or directory

为什么?我创建分支的全部目的是我可以尝试一些操作,如果不成功,可以将其丢弃.想过吗?

Why? The whole point of me creating a branch, was that I could try something, and throw it away if unsuccessful. Toughts?

推荐答案

您尚未提交删除.只有提交的更改是隔离的".

You did not commit the delete. Only committed changes are "isolated".

关于您的评论:

也许我错过了一些东西,但是在我看来,这是一种奇怪而令人讨厌的行为.

Maybe I'm missing something, but it seems to me a weird and undesirable behavior.

考虑一下:

您在 crazy-refactoring 中删除了 README ,但未提交更改.Git知道您已经更改了文件并且没有提交.为了保留您所做的更改,如果您签出另一个分支,它不会覆盖它们.
否则,当您切换回分支进行更改时,Git将无法知道所做的更改.更改将丢失,这比在另一个分支中看到更改要糟得多.

You deleted README in crazy-refactoring but did not commit the change. Git knows that you changed the file and that you did not commit it. In order to preserve your changes, it does not override them if you checkout another branch.
Otherwise there would be no way for Git to know what the changes have been when you switch back to branch you made the changes in. They would be lost, and that is far more worse than seeing the changes in the other branch.

您当前的工作目录始终是当前分支+未提交的更改(除非您将其重置).

You current working directory is always current branch + uncommitted changes (unless you reset them).

当然,当退出另一个分支时,Git可能会警告您您尚未提交更改,但是有时希望保留更改切换到另一个分支.

Of course, Git could warn you when checking out the other branch, that you have uncommitted changes, but sometimes it is desired to keep the changes and switch to another branch.

这篇关于Git:分支机构应该隔离更改,还是不应该?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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