了解结账时的git行为;预计结账时会出错,但没有收到错误

时间:2015-01-31 22:20:06

标签: git git-branch git-checkout

我的问题是结帐时的git行为,以下是阐明我不理解的行为的步骤。我创建了一个目录testdir,文件foobar各有一行文字。我做了以下事情:

git init
git add .
git commit
git branch bug

所以我有master分支和相同的bug分支。在master中,我向文件foo添加了一行文本,但不添加或提交到master,只是在本地树上进行文件更改。接下来我做:

git checkout bug

我希望收到以下错误:

   error: Your local chages to the following file would be overwritten by checkout:
     foo
  Please commit your changes or stash them before you can switch branches.

但是我确实 NOT 得到了上述错误,我的问题是git在这种情况下怎么没有错误?相反,我得到了以下内容:

  root@revision-control ~/testdir# git checkout bug
  M       foo
  Switched to branch 'bug'

我现在在bug分支,但foo文件有我在master时添加的第二行。

任何人都可以解释这里发生了什么以及为什么git让我在没有错误消息的情况下更改分支?并且文件中没有合并冲突指示符。

接下来我更改回master分支并添加然后将更改提交到foo文件(因此master文件foo有2行和{{1} } bug有一行)。我对foo中的foo进行了更改并添加了第三行文本,现在当我尝试签出master分支(与上面相同的git命令)时,我得到:

user@host ~/testdir# git checkout bug
error: Your local changes to the following files would be overwritten by checkout:
        foo
Please, commit your changes or stash them before you can switch branches.
Aborting

那么为什么这种行为现在与以前不同,我得到了错误? (这是我最初期待的行为)。

2 个答案:

答案 0 :(得分:0)

欢迎新人!

现在已经有一段时间了,但我曾经遇到过和你一样的错误:

error: Your local changes to the following files would be overwritten by checkout

我读过其中一个解决方案是删除文件(rm)然后执行结帐。否则你也可以添加并存储它们。 强制结帐如:

git checkout --force master

可能是另一种选择。

您是否尝试过此GIT - Fail to checkout to other branch when nothing to commit - when file is in gitignore链接?也许这可以帮助你进一步..

干杯。 乔伊

答案 1 :(得分:0)

根据git checkout的文件:

git checkout <branch>
Local modifications to the files in the working tree are kept, so that they can be committed to the <branch>.

应该将分支之间的切换视为应用代表它们之间差异的补丁。

在你的第一个案例中没有区别 在第二种情况下,checkout命令甚至没有尝试解决冲突。

相关问题