git checkout切换分支报错解决办法

本文介绍了在Git开发过程中,遇到未提交内容而无法直接切换分支的情况,如何使用stash功能临时保存工作并解决 checkout 错误。通过实例展示了stash的使用步骤,包括查看状态、stash存储、切换分支以及stash恢复的过程。
摘要由CSDN通过智能技术生成

git checkout切换分支报错解决办法

一、背景:

软件开发中,bug就像家常便饭一样。有了bug就需要修复,在Git中,由于分支是如此的强大,所以,每个bug都可以通过一个新的临时分支来修复,修复后,合并分支,然后将临时分支删除。

当你接到一个修复一个代号101的bug的任务时,很自然地,你想创建一个分支issue-101来修复它,但是,等等,当前正在dev上进行的工作还没有提交:

并不是你不想提交,而是工作只进行到一半,还没法提交,预计完成还需1天时间。但是,必须在两个小时内修复该bug,怎么办?

幸好,Git还提供了一个stash功能,可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作:

为什么要将工作现场存储起来,原因是不存储,没法checkout切换到别的分支工作

二、案例:
$ git status
On branch yukw               ##查看当前yukw分支有未提交内容
Your branch is ahead of 'origin/yukw' by 4 commits.
  (use "git push" to publish your local commits)

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:   test.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ff.txt

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

yukw@cs-yukw01 MINGW64 /f/yukw-project/yukw-note (yukw)
$ git checkout master           ###切换到master分支失败
error: Your local changes to the following files would be overwritten by checkout:
        test.txt
Please commit your changes or stash them before you switch branches.
Aborting

yukw@cs-yukw01 MINGW64 /f/yukw-project/yukw-note (yukw)
$ git checkout yukw
Already on 'yukw'
M       test.txt
Your branch is ahead of 'origin/yukw' by 4 commits.
  (use "git push" to publish your local commits)

yukw@cs-yukw01 MINGW64 /f/yukw-project/yukw-note (yukw)
$ git status
On branch yukw
Your branch is ahead of 'origin/yukw' by 4 commits.
  (use "git push" to publish your local commits)

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:   test.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ff.txt

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

yukw@cs-yukw01 MINGW64 /f/yukw-project/yukw-note (yukw)
$ git stash                 ###将yukw分支修改内容存储起来
Saved working directory and index state WIP on yukw: bfce54d 添加cc

yukw@cs-yukw01 MINGW64 /f/yukw-project/yukw-note (yukw)
$ git checkout master         ###切换master成功
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

yukw@cs-yukw01 MINGW64 /f/yukw-project/yukw-note (master)

当修改完bug后,我们再回到dev分支继续之前未开发完成的工作

$ git checkout yukw          ###切换到之前的开发分支
Switched to branch 'yukw'
Your branch is ahead of 'origin/yukw' by 4 commits.
  (use "git push" to publish your local commits)

yukw@cs-yukw01 MINGW64 /f/yukw-project/yukw-note (yukw)
$ git status
On branch yukw
Your branch is ahead of 'origin/yukw' by 4 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ff.txt

nothing added to commit but untracked files present (use "git add" to track)

yukw@cs-yukw01 MINGW64 /f/yukw-project/yukw-note (yukw)
$ git stash list           ###查看工作现场存储到哪了
stash@{0}: WIP on yukw: bfce54d 添加cc

yukw@cs-yukw01 MINGW64 /f/yukw-project/yukw-note (yukw)
$ git stash pop       ###恢复存储现场
On branch yukw
Your branch is ahead of 'origin/yukw' by 4 commits.
  (use "git push" to publish your local commits)

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:   test.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ff.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (8fefd98dc2609cc58ea20e926cd0aeec99e0bdd7)

参考:https://www.liaoxuefeng.com/wiki/896043488029600/900388704535136

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值