git 带着修改checkout 到其他分支

正常情况下,如果所在分支有修改想工切换到其他分支就会报错:

 zhangjie$ git checkout mastern

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

first

Please commit your changes or stash them before you can switch branches.

Aborting


但是如果其他分支名和一个此目录的一个文件名同名的话,会发生奇妙的事。

比如我有如下文件

 zhangjie$ ls

README.md first mn


我的修改为删除一行

zhangjie$ git diff

diff --git a/first b/first

index 9465807..f9c4725 100644

--- a/first

+++ b/first

@@ -1,4 +1,3 @@

-it i

 oi

 

我的分支为

git branch

  masn

* master

  mastern

  mn

从上面可看出我有一个分支名为mn,也有一个文件名为mn,此时我也有修改,我执行checkout

zhangjie$ git checkout mn

M first

Switched to branch 'mn'

Your branch is up-to-date with 'origin/master'.

zhangjiedeMacBook-Pro:gitLearning zhangjie$ git status

On branch mn

Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:

  (use "git add <file>..." to update what will be committed)

  (use "git checkout -- <file>..." to discard changes in working directory)


modified:   first


no changes added to commit (use "git add" and/or "git commit -a")

zhangjiedeMacBook-Pro:gitLearning zhangjie$ git diff

diff --git a/first b/first

index 9465807..f9c4725 100644

--- a/first

+++ b/first

@@ -1,4 +1,3 @@

-it i

 oi

 

可以看到我在master里的修改没有提交,没有stash,但是我checkout 到了mn这个分支,并且把在master的修改带过来了。

我在mn分支提交后再切回到master,master里没有修改了

zhangjiedeMacBook-Pro:gitLearning zhangjie$ git commit -m 'd ling'

[mn 317c0c3] d ling

 1 file changed, 1 deletion(-)

zhangjiedeMacBook-Pro:gitLearning zhangjie$ git checkout master

Switched to branch 'master'

Your branch is up-to-date with 'origin/master'.

zhangjiedeMacBook-Pro:gitLearning zhangjie$ git status

On branch master

Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean

zhangjiedeMacBook-Pro:gitLearning zhangjie$ 

比较奇妙,不仅在有修改的时候能切换分支,且可以把修改的内容merge过去,感觉是因为分支名和文件名相同,被认为是checkout file,但实际执行的却是checkout branch,但是如果到新的分支,原来的内容就丢失了,所以就把原分支的修改merge到新分支了。有谁明白也不吝赐教。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值