情景
- 根据用户的新需求,新建分支并在此分支上开展工作
- 新需求还未完成,线上分支出现紧急问题需要处理
- 为此紧急问题新建一个分支,修复后推送至线上分支
解决方式
提交
将已完成的部分先提交(git否则会将在当前分支已修改但是未提交的内容带到新切换的分支上),切换到线上分支,新建分支进行修复
-
git add .
+git commit
- 修复完成后,切换回该分支即可
暂存
将已完成的部分先暂存(git stash
),切换到线上分支,新建分支进行修复
-
git add .
+git stash
- 修复完成后,切换回该分支
git stash pop
恢复当初的暂存
【推荐】使用 git worktree
git worktree add ../文件夹 <branch name>
相当于把分支<branch name>checkout进本地的特定文件夹📁中(进入该文件夹就相当于切换到该分支)
[图片上传失败...(image-2d67d0-1655452537589)]
git worktree list
查看分支列表
[图片上传失败...(image-27d7f9-1655452537589)]
aqin1012@aqin1012deMacBook-Pro git-test % git worktree list
/Users/aqin1012/Java/test/git-test 40a8312 [test]
/Users/aqin1012/Java/test/git-test/devDir 40a8312 [dev]
git worktree remove 文件夹
删除worktree
aqin1012@aqin1012deMacBook-Pro git-test % git worktree remove devDir
aqin1012@aqin1012deMacBook-Pro git-test % git worktree list
/Users/aqin1012/Java/test/git-test 40a8312 [test]