Git会自动将来自不同分支的更改合并到主 [英] Git is automatically merging changes from a different branch to master

查看:340
本文介绍了Git会自动将来自不同分支的更改合并到主的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Git分支的问题。每当我对分支进行更改时,即使我没有调用显式合并命令,所有这些更改都会反映在主分支中。



例如,



我创建了一个dashboard分支 git checkout -b dashboard



那么我已经对我的一个文件(比如routes.rb)进行了修改,现在我切换到master git checkout master



现在当我打开routes.rb时,我可以看到仪表板分支的更改。为什么?我有一些不应该在那里的git设置吗?

解决方案

当您进行更改时,这些更改仅存在于您的工作树中直到您提交它们的时候。



切换分支时,Git会将工作树中的更改传送到新的结帐。当你注意到你在错误的分支上工作时,这通常很有帮助。

甚至有一个最近关于这个意外行为的讨论在Git邮件列表上。引用Junio:


J.V。 gmail.com>写道:


好吧,工作树对我来说是一个新词。我认为我们在被隔离的
沙箱称为分支,并且在分支中进行的更改将保留在
分支中,无论如何。


不要将分支看作是孤立的沙箱



相反,分支是独立国家被记录的$ b $ em 。

记录的状态只存在于git存储库中,并使用它的
内容(例如在浏览器或浏览器中查看,在编辑器中编辑,运行
编译器,...),您需要实现文件系统某处
分支的内容。
文件系统上的这样一组文件构成了工作树。这样做的行为称为
检查分支。
[...]







编辑



以防万一上面的链接变为无效



问题



 意外的git行为

---
#首先创建一个本地git仓库

$ mkdir gitexample
$ git config - 全球用户名我的名字
$ git config --global user.emailme@me.com
$ git init
$ git add。
$ git commit -m'初始提交'

#创建/编辑一个空文件
$ vi readme.txt

#添加一行:这是在主分支中添加的。
$ git commit -a

#创建并签出一个新分支(来自master)
$ git分支测试
$ git结帐测试

#编辑readme.txt文件,不要提交
#添加文本:这是在测试分支中添加的,保存并退出
$ vi readme.txt

#现在切换回主master
$ git checkout master
$ cat readme.txt

#你会在master看到两行。

问题#1:
为什么这行添加到* master分支中?


---甚至更令人惊讶
在master分支中,现在做一个提交
$ git commit -a

自述文件.txt(你会在测试分支中看到现在在master中的行)

问题#2:
为什么会发生这种情况?

#现在切换回测试分支
$ git checkout test
$ cat readme.txt

您只会看到一行:这已添加到主分支

问题#3:
为什么会发生这种情况?

而不是在该分支中添加的行:这已添加到测试分支中< =此行已消失

这是什么原因?

1)为什么我看到在master分支中master分支中的未提交更改?
2)为什么如果我将它们提交给主人,在他们所在的分支中消失?

这令人困惑,我认为* master分支将保持不变。这将解决问题#2。



回覆



 在星期五,2011年11月11日在12:55:04 PM -0800,Jvsrvcs写道:
>意外的git行为
>
[...切换本地修改的分支...]
> #你会在主人看到两行。
>
>问题#1:
>为什么这行添加在* master分支中?
>

不是。该行被添加到工作目录中。当您
切换分支时,如果当前分支尖端中的文件与目标分支尖端中的
文件没有差异,则可以安全地保留
您的本地更改,所以git的确如此。这是为了支持
的用例,当错误的分支被检出并且想要
改变到正确的分支时,你开始编辑一个文件。

>
> ---甚至更惊人
>在master分支中,现在做一个提交
> $ git commit -a
>
> cat readme.txt(你会看到现在已经添加到
>测试分支中的主行)
>
>问题#2:
>为什么发生这种情况?
... [显示其余部分的引用]
... [显示其余部分引用]
因为你告诉git使用该修改提交文件。

>
> #现在切换回测试分支
> $ git checkout test
> $ cat readme.txt
>
>你只会看到一行:这是添加到主分支
>
>问题#3:
>为什么发生这种情况?

因为'test'分支中的文件只有该行。正如你自己说的
,你编辑了文件但没有提交。

>
>而不是在该分支中添加的行:这已添加到测试分支
> < =这一行消失了

此外,该行并未添加到任何分支中,而是添加到工作
目录中。活跃的分支是'测试',但并不神奇地意味着
未定义的变化伴随着它。


I am having an issue with Git branching. Whenever I make changes to a branch, all those changes get reflected in master branch even though I haven't invoked explicit merge command.

For example,

I created a "dashboard" branch git checkout -b dashboard

then I have made changes in one of my file(say routes.rb) and now I switched to master git checkout master

Now when I open routes.rb, i can see the changes from dashboard branch. Why? Do I have some git settings that should not be there?

解决方案

When you make changes, those changes only exist in your working tree up until to the point when you commit them.

When you switch branches, Git will carry changes in your worktree over to the new checkout. This is often helpful when you notice that you were working on the wrong branch.

There's even a recent discussion about this "unexpected" behavior on the Git mailing list about this. To quote Junio:

"J.V." gmail.com> writes:

OK so "work tree" is a new term for me. I thought we were in isolated sandboxes called "branches" and changes made in a branch would stay in that branch regardless.

Do not think of "branches" as isolated sandboxes.

Rather, "branches" are where the independent states are to be recorded.

The recorded states only exist in the git repository, and to use its contents (e.g. view in the pager or browser, edit in the editor, run the compiler on,...), you need to materialize the contents of the branch somewhere on the filesystem. Such a set of files on the filesystem form the working tree. The act of doing so is called "checking out a branch". […]


Edit

Just in case if above link becomes void

Problem

Unexpected git behaviour 

--- 
# First create a local git repo 

$mkdir gitexample 
$git config --global user.name "my name" 
$git config --global user.email "me@me.com" 
$git init 
$git add . 
$git commit -m 'initial commit' 

# Create/Edit an empty file 
$vi readme.txt 

# add a single line: "this was added in the master branch." 
$git commit -a 

# create and checkout a new branch (from master) 
$git branch test 
$git checkout test 

# edit the readme.txt file and do not commit 
# add the text:  "this was added in the test branch.", save and exit 
$vi readme.txt 

#now switch back to master 
$git checkout master 
$cat readme.txt 

#You will see both lines in the master.   

Question #1: 
        Why was this line added in the *master branch? 


--- even further surprising 
In the master branch, now do a commit 
$git commit -a 

cat readme.txt ( you will see the line in the master now that was added in the test branch ) 

Question #2: 
        Why did this happen? 

# Now switch back to the test branch 
$git checkout test 
$cat readme.txt 

You will only see the one line: "This was added in the master branch" 

Question #3: 
        Why did this happen? 

and NOT the line added in that branch: "this was added in the test branch" <= this line is gone 

What is the reason for this? 

1) Why do I see uncommitted changes in the branches made off master in the master branch? 
2) Why, if I commit them in the master, do the disappear in the branch in which they were made? 

This is confusing, I would think the * master branch would be left untouched.  This would solve issue #2. 

Reply

On Fri, Nov 11, 2011 at 12:55:04PM -0800, Jvsrvcs wrote: 
> Unexpected git behaviour 
> 
[ ... switch branches with local modifications ...] 
> #You will see both lines in the master.   
> 
> Question #1: 
> Why was this line added in the *master branch? 
> 

It wasn't. that line was added in the working directory. When you 
switch branches, if the file in the tip of the current branch and the 
file in the tip of the target branch don't differ, it's safe to keep 
your local changes, so git does. This is to support the use-case where 
you start editing a file when the wrong branch is checked out and want 
to change to the right one. 

> 
> --- even further surprising 
> In the master branch, now do a commit 
> $git commit -a 
> 
> cat readme.txt ( you will see the line in the master now that was added in 
> the test branch ) 
> 
> Question #2: 
> Why did this happen?
... [show rest of quote]
... [show rest of quote]
Because you told git to commit the file with that modification in it. 

> 
> # Now switch back to the test branch 
> $git checkout test 
> $cat readme.txt 
> 
> You will only see the one line: "This was added in the master branch" 
> 
> Question #3: 
> Why did this happen? 

Because the file in the 'test' branch only has that line. As you said 
yourself, you edited the file but didn't commit. 

> 
> and NOT the line added in that branch: "this was added in the test branch" 
> <= this line is gone 

Again, that line wasn't added in any branch but in the working 
directory. The active branch was 'test', but doesn't magically mean 
that uncommitted changes travel with it. 

这篇关于Git会自动将来自不同分支的更改合并到主的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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