Git:在没有提交的情况下创建新的分支时,对主分支的更改也会分支 [英] Git: When creating a new branch without commit, changes to master changes branch too
问题描述
git init
git add *
git commit -m'初始提交'
然后创建一个新的分支:
git checkout -b new_branch
Go回复主人:
git checkout master
如果我现在更改我的文本文件,然后在未提交的情况下运行:
git checkout new_branch
new_branch
中的文件是也改变了!为什么是这样?它们都指向相同的提交(相同的哈希),但它仍然感到违反直觉,即对 master
的更改也会改变 new_branch
。当我期望 new_branch
与创建时相同时,我对git的理解是否有缺陷?
(PS :我不是在问如何创建一个新的分支,或者如何使用分支来保存我的工作,我只是对这种特定情况下的gits行为感到困惑,并且希望更好地理解它的逻辑)
因为您的更改位于工作目录/暂存区域中,所以它们未提交。当你检查别的东西时,它们将被结转,除非在结帐将被中止时发生冲突。很方便。
Let's say I have a brand new project with a text file. I run:
git init
git add *
git commit -m 'Initial commit'
Then create a new branch:
git checkout -b new_branch
Go back to master:
git checkout master
If I now change my text file, and then, without committing, run:
git checkout new_branch
The file in new_branch
is also changed! Why is this? They're both pointing to the same commit (same hash), but it still feels counterintuitive that changes on master
also changes new_branch
. Is my understanding of git flawed when I expect new_branch
to be the same as when it was created?
(PS: I'm not asking for how to create a new branch or how to save my work using a branch, I'm just puzzled by gits behaviour in this specific circumstance, and want to understand its logic better)
Because your changes are in the working directory/staging area, so they aren't committed. And when you check something else out they will be carried over unless a conflict when the checkout will be aborted. Very convenient.
这篇关于Git:在没有提交的情况下创建新的分支时,对主分支的更改也会分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!