Git分支无法正常运行 [英] Git branch is not working as I am expecting

查看:122
本文介绍了Git分支无法正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我不熟悉git ..我以为我了解分支的概念,但是现在我很困惑.

Okay so I am new to using git..and I thought I understood the concept of branching, but now I am confused.

让我说我有一个大师.

我创建了一个名为branch1的分支.

I create a branch called branch1.

git checkout -b branch1

master
*branch1

我对branch1进行了一些更改,但是我意识到我必须在完成对branch1的工作之前要做一些其他事情.

I made some changes on branch1, but I realized I have to do something else before finishing my work on branch1.

所以我回到大师那里

git checkout master

*master
branch1

并创建一个名为branch2的新分支.

and create a new branch called branch2.

git checkout -b branch2

git checkout -b branch2

master
branch1
*branch2

由于我没有在branch1上提交更改,所以我希望我的branch2将没有branch1代码...但是我看到的是对branch1所做的更改.

Since I didn't commit my changes on branch1, I am expecting my branch2 will not have branch1 codes...but I am seeing the changes made from branch1.

我误会了什么吗?

推荐答案

尽管进行更改时您在branch1中,但是直到您提交更改,这些更改才真正在"branch1中".

Although you were in branch1 when you made the changes, those changes are not really "in" branch1 until you commit them.

来自$ git help checkout:

对工作树中文件的本地修改 保留,以便可以将它们提交给< branch>.

Local modifications to the files in the working tree are kept, so that they can be committed to the <branch>.

另一方面,如果您将更改提交到branch1 之后再切换回master,那么当您$ git checkout master时,git会将工作目录中的文件还原到快照中正如您所期望的那样,师父指向了.

If, on the other hand, you commit your changes to branch1 before switching back to master, then when you $ git checkout master, git will revert the files in your working directory back to the snapshot that master points to, as you are expecting.

如果出于某种原因您确实想避免将更改提交到branch1,但仍在没有这些更改的情况下仍在其他分支上工作,则可以考虑使用$ git stash存储更改,然后切换到新分支并执行工作,然后切换回branch1并执行$ git stash pop以找回已保存的更改.

If for some reason you really want to avoid committing your changes to branch1, but still work on a different branch without those changes, you might consider using $ git stash to stash your changes, then switch to your new branch and do the work you need to, then switch back to branch1 and do $ git stash pop to get back your stashed changes.

这篇关于Git分支无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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