gpt4 book ai didi

Git 自动 merge 来自不同分支的更改到 master

转载 作者:IT王子 更新时间:2023-10-29 01:05:32 25 4
gpt4 key购买 nike

我在使用 Git 分支时遇到问题。每当我对分支进行更改时,所有这些更改都会反射(reflect)在主分支中,即使我没有调用显式 merge 命令也是如此。

例如,

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

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

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

最佳答案

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

当您切换分支时,Git 会将工作树中的更改转移到新的 checkout 处。当您发现自己在错误的分支上工作时,这通常很有帮助。

甚至还有一个 recent discussion about this “unexpected” behavior在关于这个的 Git 邮件列表上。引用朱尼奥的话:

"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.

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

相反,“分支”是独立国家所在的地方 记录

记录的状态只存在于git仓库中,使用它 内容(例如在寻呼机或浏览器中查看,在编辑器中编辑,运行 编译器,......),你需要具体化的内容 在文件系统的某处分支。上这样一组文件 文件系统形成工作树。这样做的行为被称为 “ checkout 一个分支”。 […]


编辑

以防万一以上链接失效

问题

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 自动 merge 来自不同分支的更改到 master,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8111991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com