为什么分支中的未提交更改会影响主分支? [英] Why does uncommitted change in a branch affect the master branch?
问题描述
我是git的新手,我试着用它来理解概念,特别是分支和合并。
所以这里是我的设置, >
我有一个主分支,我用'master'文本创建了一个主文本文件。
现在我做了 git checkout - b分支
并创建一个分支。
我编辑分支的'父'文件并添加一行文本。
现在如果我提交这个改变并切换回主,它不会影响,因为它不应该,因为分支的改变不应该反映在主分支。
但是,如果我离开分支未提交,并切换到主,这种变化反映在那里和git对待主文件作为编辑,当我做
git status -s
它显示主文件带有M。
任何人都可以向我解释未落实的变更是如何反映在master分支中的吗?
G它会在检出另一个分支时保留未提交的更改,这非常实用。
你可以看到这是因为未提交的更改只属于你的工作副本,而不属于任何分支或提交。他们是独立的。当你在分支中提交更改时,如果签出的文件版本不同,他们当然会改变。
这种行为的唯一例外是,如果分支更改会将未提交的文件带入不同的版本,结算将被取消:
A - B - 特征
-C $ master
假设在<$ B中提交B c $ c> feature 分支将一行更改为 foo.txt
,并且您拥有 master
分支签出。您已对 foo.txt
进行了不同的更改。
-
您在
master
和结帐功能
<$ p $git add foo.txt
git commit -mchanged foo.txt
git checkout feature
这里没有问题,更改记录在
master
中,当您转到功能 code>
foo.txt
会相应更改。 尝试检出
特性
,然后Git将打印一条适当的消息,并保持 master
分支被检出: git结账功能
错误:您在本地对以下文件所做的更改将被
通过结帐覆盖:
foo.txt
请提交您的更改或隐藏它们
,然后才能切换分支。正在中止
I am new to git and I am trying to experiment with it to understand the concepts particularly the branching and merging.
So here is my set up,
I have a master branch, I create a master text file with 'master' text.
Now I do git checkout -b branch
and create a branch.
I edit the branch 'parent' file and add one line of text.
Now If I commit this change and switch back to master, It won't affect as It shouldn't, as Branch changes should not reflect in the master branch.
However If I leave the branch uncommitted and switch to master, This change reflect there and git treats master file as edited, When I do
git status -s
It shows that master file with M.
Can anyone explain to me how the uncommitted changes are reflecting in the master branch?
Git keeps your uncommitted changes when checking out another branch, which is very practical.
You can see this as uncommitted changes "belong" only to your working copy, and not to any branch or commit. They are independent. When you will commit the changes in a branch, they will of course change if the checkout has a different version for the file.
The only exception to this behaviour is if the branch change brings an uncommitted file to a different version, it which case the checkout is canceled:
A--B - feature
\
-C - master
Let's say commit B in the feature
branch changes a line to foo.txt
, and that you have the master
branch checked out. You have made a different change to foo.txt
.
You commit the change in
master
and checkoutfeature
git add foo.txt git commit -m "changed foo.txt" git checkout feature
Here no problem, the change is recorded in
master
and when you go tofeature
foo.txt
is changed accordingly.If you don't commit and try to checkout
feature
, then Git will print an appropriate message, and keep themaster
branch checked out:git checkout feature
error: Your local changes to the following files would be overwritten by checkout:
foo.txt
Please, commit your changes or stash them before you can switch branches. Aborting
这篇关于为什么分支中的未提交更改会影响主分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!