在git中切换分支-我何时会收到“您的本地更改无法切换分支”。 [英] switching branches in git - when will i get "You have local changes cannot switch branches."?

查看:545
本文介绍了在git中切换分支-我何时会收到“您的本地更改无法切换分支”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能的副本:

git:切换分支并忽略所有更改而无需提交。

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 :

我有一个存储库,文件名为version.txt,其中包含文本 1 :

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


git checkout -b new

git checkout -b new

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

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

git checkout master(这是怎么工作的?我还没有分阶段对新内容进行更改)

git checkout master ( how come this works ? I have not stages\commited my changes on 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." ?

谢谢,
Ran

Thanks, Ran

推荐答案

这很简单,如果您在文件中有更改,如果您更改到特定的分支,将对其进行修改。

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

示例:

$ 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

如果您在修改文件之前将testbranch基于master,则它将起作用,因为文件不会被修改。

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

另一种放置方式是,如果分支分支不同,而您修改了分支文件,它将无法正常工作。

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

这篇关于在git中切换分支-我何时会收到“您的本地更改无法切换分支”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆