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