在 git 中切换分支 - 我什么时候会收到“您有本地更改无法切换分支。”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6638937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:40:30  来源:igfitidea点击:

switching branches in git - when will i get "You have local changes cannot switch branches."?

gitbranchgit-branchgit-checkout

提问by ran wainstein

Possible Duplicates:
git: Switch branch and ignore any changes without committing.
Git branches behaving strangely

可能的重复项:
git:切换分支并忽略任何更改而不提交。
Git 分支行为异常

I know that the general recommendation is to have a clean status before switching branches in git. ( stash or park-commit ). I'm trying to understand when will I get "You have local changes cannot switch branches" , I can't follow the logic :

我知道一般的建议是在 git 中切换分支之前有一个干净的状态。( stash 或 park-commit )。我想知道我什么时候会收到“您有本地更改无法切换分支”,我无法遵循以下逻辑:

I have a repo, with a file called version.txt, contains the text "1" :

我有一个 repo,其中包含一个名为 version.txt 的文件,其中包含文本 "1" :

git checkout -b new

echo 2 >> version.txt (working dir is not dirty, modified the file)

git checkout master ( how come this works ? I have not stages\commited my changes on new )

git checkout -b 新建

echo 2 >> version.txt(工作目录不脏,修改文件)

git checkout master(这是怎么工作的?我还没有阶段\提交我对 new 的更改)

same happens if I delete the file content in my new branch, or stage the file first.

如果我删除新分支中的文件内容,或者先暂存文件,也会发生同样的情况。

Can someone help me to understand when will i get the "You have local changes cannot switch branches." ?

有人可以帮助我了解我什么时候会收到“您有本地更改无法切换分支”的消息。?

Thanks, Ran

谢谢,冉

回答by Ulrich Dangel

It's quite simple, if you have changes in a file which will be modified if you change to a specific branch.

这很简单,如果您对文件进行了更改,如果您更改到特定分支,该文件将被修改。

Example:

例子:

$ git init
Initialized empty Git repository in /tmp/asadfasd/.git/
$ echo 1 > bar
$ git commit -am "commit 1 master"
[master (root-commit) 55da003] commit 1
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 bar
$ git co -b testbranch
Switched to a new branch 'testbranch'
$ git co master
Switched to branch 'master'
$ echo 2 >> bar
$ git commit -am "commit 2 master"
[master c6dc6d9] commit 2 master
 1 files changed, 1 insertions(+), 0 deletions(-)
$ git co testbranch
Switched to branch 'testbranch'
$ echo 3 >> bar
$ git co master
error: Your local changes to the following files would be overwritten by checkout:
bar
Please, commit your changes or stash them before you can switch branches.
Aborting

If you would rebase testbranch on master before modifying the file it would work because the file wouldn't be modified.

如果您在修改文件之前将 testbranch 重新设置在 master 上,它将起作用,因为该文件不会被修改。

Another way to put it, if the branches are diverged and you modify the diverged files it won't work.

换句话说,如果分支是分叉的并且您修改了分叉的文件,它将不起作用。

回答by manojlds

When master and test HEAD are same, local changes in test are applied to master when you switch back.

当 master 和 test HEAD 相同时,当您切换回来时,test 中的本地更改将应用​​于 master。

If however they are not, say you had an extra commit in master after you created test or an extra commit in test, you will get the warning.

但是,如果它们不是,假设您在创建测试后在 master 中进行了额外的提交或在测试中进行了额外的提交,您将收到警告。



( just remembered that I had answered this before - pasting my old answer here and voting to close this question as duplicate)

(只记得我之前已经回答过这个问题 - 在这里粘贴我的旧答案并投票关闭这个问题作为重复)

When you are switching branch and the files are only locally modified, Git will not give you the warning / message ( and will merge the changes into the other branch). For example, you have your repo on master, create a branch temp, have a local modification on the file. Now, when you switch to master you won't get the message. If on the other hand you makes changes in temp and commit them ( that is temp diverges from master ) and then you have local modifications, it will give you that message when you switch to master

当您切换分支并且文件仅在本地修改时,Git 不会给您警告/消息(并将更改合并到另一个分支中)。例如,你在 master 上有你的 repo,创建一个分支临时,对文件进行本地修改。现在,当您切换到 master 时,您将不会收到消息。另一方面,如果您在 temp 中进行更改并提交它们(即 temp 与 master 不同),然后您进行了本地修改,则当您切换到 master 时,它会给您该消息

Git branches behaving strangely

Git 分支行为异常

回答by Richard Hansen

When switching branches, Git only updates the parts of the index and working directory that differ from the revision you are switching away from. If a file doesn't differ between the two revisions, git checkoutdoesn't even look to see if it was modified.

切换分支时,Git 仅更新索引和工作目录中与您要切换的修订版本不同的部分。如果文件在两个修订版之间没有差异,git checkout甚至不查看它是否被修改。

In your example, the contents of version.txtis the same in each branch. Thus, Git happily ignores any uncommitted modifications to that file when switching between the two.

在您的示例中,version.txt每个分支中的内容都相同。因此,当在两者之间切换时,Git 很乐意忽略对该文件的任何未提交的修改。