繁体   English   中英

Git会自动将更改从不同的分支合并到主分支

[英]Git is automatically merging changes from a different branch to master

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

例如,

我创建了一个“仪表板”分支git checkout -b dashboard

然后我在我的一个文件(比如routes.rb)中进行了更改,现在我切换到了主git checkout master

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

进行更改时,这些更改仅存在于工作树中,直到提交它们为止。

当您切换分支时,Git会将您的工作树中的更改带到新的结帐。 当您发现自己正在使用错误的分支时,这通常很有用。

最近有关于 Git邮件列表中关于此的“意外”行为的讨论 引用Junio:

“JV”gmail.com>写道:

好的,所以“工作树”对我来说是一个新名词。 我以为我们在被称为“分支”的孤立沙箱中,并且在分支中进行的更改将保留在该分支中。

不要将“分支”视为孤立的沙箱

相反,“分支”是记录独立状态的地方。

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


编辑

以防上述链接变为无效

问题

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. 

回复

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”文件(任何分支或主文件),你只是编辑本地文件,或者调用它的“工作目录”。

“git”文件(你提交的东西)都在.git目录中。 布局与您的文件夹匹配,这也是分支存储的位置。 旁注:它存储实际文件(压缩),而不像版本控制工具(如svn)存储增量(差异)

因此,在编辑文件时,您实际上并未编辑主文件或分支文件,而只是编辑文件。 如果您没有提交但是然后切换分支,您仍然会拥有该文件,即使您已经“切换”到新分支,它的更改也会显示。 这通常是人们最初的惊喜。

这里最好的建议是在切换分支之前提交/忽略/丢弃所有更改以避免这些问题。 此外,像gitx(Mac)m和gitg(Ubuntu)这样的工具使那些喜欢gui的人更容易完成这些任务,并且他们对这些问题也有很好的警告。

在上面的git status任何一点都非常有用,可以告诉你当前没有提交给任何git仓库的东西(无论是master还是分支)

gustavotkg也为这些问题使用git stash提供了很好的建议。

如果你需要切换到master分支而不提交当前分支,你可以使用git stash

git stash # all changes will be queued
git checkout master
# do whatever you need in master
git checkout dashboard
git stash pop # get all changes queued back to branch

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM