git 切换分支时相互影响的问题

问题

  1. develop 分支上有 add 但还没有 commit 的文件。
  2. 切换到 other 分支后,这些文件在 other 分支下也能被看到。
  3. 为什么,怎么解决

小知识

如何隐藏当前暂存区内容

# 初始化一个 git 库
$ git init
$ ll ./ > aa.txt
$ git add .
$ git commit -m "committed aa..txt"

# 隐藏当前分支的工作现场(必须添加到暂存区)
$ ll ../ > bb.txt
$ git add .
$ git stash

# 可以多次隐藏
$ ll ../../ > cc.txt
$ git add .
$ git stash

# 查看隐藏的工作现场,最近保存的在列表的最上面
$ git stash list
stash@{0}: WIP on develop: 96ac618 committed aa.txt
stash@{1}: WIP on develop: 96ac618 committed aa.txt

# 恢复现场,两种方法

## 第一种, git stash apply 恢复, 然后要手工删除 stash 内容, git stash drop删除
### 恢复最近保存的现场
$ git stash apply
### 恢复指定的现场
$ git stash apply stash@{1}
$ git stash apply stash@{0}
### 清空隐藏的现场
$ git stash drop

## 第二种, git stash pop 恢复,自动会清除 stash 内容
### 恢复最近保存的现场
$ git stash pop
### 恢复指定的现场
$ git stash pop stash@{0}

解决过程

# 切回 develop 分支
$ git checkout develop

# 删除 other 分支
$ git branch -d other

# 把文件藏起来,让工作区看起来干净
$ git stash

# 再创建 other 分支
$ git checkout -b other

# 验证,工作区很干净
$ git status
On branch other
nothing to commit, working tree clean


# 在 other 分支上干完活后,合入 develop 分支,删除 other 分支,
## 再恢复之前隐藏的文件
$ git stash apply stash@{0}

## 删除隐藏的备份
git stash drop

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值