我遇到了Git分支的问题。每当我对分支进行更改时,即使我没有调用显式合并命令,所有这些更改也会反映在主分支中。
例如,
我创建了一个“信息中心”分支git checkout -b dashboard
然后我对我的一个文件(比如routes.rb)进行了更改,现在我切换到了主文件git checkout master
现在当我打开routes.rb时,我可以看到仪表板分支的变化。为什么?我有一些不应该存在的git设置吗?
答案 0 :(得分:28)
进行更改时,这些更改仅存在于工作树中,直到您提交更改为止。
当您切换分支时,Git会将您的工作树中的更改带到新的结帐中。当您发现自己正在使用错误的分支时,这通常很有用。
Git邮件列表上甚至还有一个recent discussion about this “unexpected” behavior。引用Junio:
“J.V。” 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.
答案 1 :(得分:12)
当您在工作目录中编辑文件时,您没有编辑&#34; git&#34;此时的文件(任何分支或主文件),您只是编辑本地文件,或者#34;工作目录&#34;因为它被称为。
&#34; git&#34;文件(你提交的东西)都在.git目录中。布局与您的文件夹匹配,这也是分支存储的位置。 旁注:它存储实际文件(压缩),而不像存储delta(差异)的svn等版本控制工具
因此,在编辑文件时,您实际上并未编辑主文件或分支文件,而只是编辑文件。如果你没有提交但是然后切换分支,你仍然会拥有该文件,即使你已经切换了它,它的变化也是可见的。到新的分支。这通常是人们最初的惊喜。
这里最好的建议是在切换分支之前提交/忽略/丢弃所有更改以避免这些问题。此外,像gitx(Mac)m和gitg(Ubuntu)这样的工具使那些喜欢gui的人更容易完成这些任务,并且他们对这些问题也有很好的警告。
在上面git status
中的任何一点都是非常有用的,可以告诉你当前没有提交给任何git仓库的东西(无论是master还是分支)
gustavotkg也使用git stash
为这些问题提供了很好的建议。
答案 2 :(得分:9)
如果您需要切换到主分支而不提交当前分支,则可以使用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