1:写在前面

在工作中经常会有这样的场景,线上发现紧急bug,需要切换到master进行修复,然后上线,但是此时我们还在开发分支,且因为一些原因本地的修改还不能提交,此时呢,如果我们直接切换分支到master的话会将分支修改的内容自动合并到master,这并不是我们想要的效果,本文看下如何处理这种情况。

2:测试

  • 查看当前分支
E:\workspace-idea\testtag>git branch -vv
  master                      a85d924 [origin/master] add aa txt
* testtag-v1-new-branch-local a85d924 [origin/testtag-v1-new-branch] add aa txt

可以看到本地分支和远程分支的对应关系,此时我们在本地分支testtag-v1-new-branch-local,先看下当前文件的内容:

E:\workspace-idea\testtag>dir
 驱动器 E 中的卷是 新加卷
 卷的序列号是 96D4-0486

 E:\workspace-idea\testtag 的目录

2021/12/12 周日  16:06    <DIR>          .
2021/12/12 周日  16:06    <DIR>          ..
2021/12/12 周日  16:06                 8 aa.txt      
2021/12/12 周日  16:06            10,356 LICENSE     
2021/12/12 周日  16:06               987 README.en.md
2021/12/12 周日  16:06             1,351 README.md   
               4 个文件         12,702 字节
               2 个目录 457,044,774,912 可用字节     
  • 分支执行一些修改

假设我们修改aa.txt增加内容巴拉巴拉喇叭喇叭,如下:

$ cat aa.txt 
111111
巴拉巴拉喇叭喇叭

然后增加一个新文件滴滴滴.txt,内容为滴滴滴,如下:

$ cat 滴滴滴.txt
滴滴滴

此时,遇到了线上问题,需要切换到主干进行修复,先看下此时分支的内容:

JHP+Administrator@jhp MINGW64 /e/workspace-idea/testtag (testtag-v1-new-branch-local)
$ ls
aa.txt  LICENSE  README.en.md  README.md  滴滴滴.txt
$ git add 滴滴滴.txt
warning: LF will be replaced by CRLF in 滴滴滴.txt.
The file will have its original line endings in your working directory
$ git status
On branch testtag-v1-new-branch-local
Your branch is up to date with 'origin/testtag-v1-new-branch'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   "\346\273\264\346\273\264\346\273\264.txt"

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   aa.txt
  • 暂存分支修改

在开发分支执行暂存--该操作很关键,如下:

$ git stash
Saved working directory and index state WIP on testtag-v1-new-branch-local: a85d924 add aa txt

JHP+Administrator@jhp MINGW64 /e/workspace-idea/testtag (testtag-v1-new-branch-local)
$ git stash show
 aa.txt                                     | 1 +
 "\346\273\264\346\273\264\346\273\264.txt" | 1 +
 2 files changed, 2 insertions(+)
  • 切换到主干

此时我们已经将分支的修改暂存了,然后切换到主干修改紧急bug:

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

JHP+Administrator@jhp MINGW64 /e/workspace-idea/testtag (master)
$ ls
aa.txt  LICENSE  README.en.md  README.md

JHP+Administrator@jhp MINGW64 /e/workspace-idea/testtag (master)
$ cat aa.txt
111111
  • 在主干修复紧急bug

可以看到此时主干完全没有受到分支修改的影响,此时我们在主干修改bug,比如在aa.txt增加内容门店端的非跨组织流程收不到代办,需要周末加班处理,如下:

$ cat aa.txt
111111
门店端的非跨组织流程收不到代办,需要周末加班处理

好的,紧急bug修复完毕了,就push:

JHP+Administrator@jhp MINGW64 /e/workspace-idea/testtag (master)
$ git commit -am'门店端的非跨组织流程收不到代办,需要周末加班处理'
[master 57b8095] 门店端的非跨组织流程收不到代办,需要周末加班处理
 1 file changed, 1 insertion(+)

JHP+Administrator@jhp MINGW64 /e/workspace-idea/testtag (master)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 422 bytes | 422.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/dongsir2020/testtag.git
   a85d924..57b8095  master -> master

接下来就上线就行了,然后我们就可以切回到分支继续之前的开发了。

  • 切回到分支继续开发
$ git checkout testtag-v1-new-branch-local
Switched to branch 'testtag-v1-new-branch-local'
Your branch is up to date with 'origin/testtag-v1-new-branch'.

JHP+Administrator@jhp MINGW64 /e/workspace-idea/testtag (testtag-v1-new-branch-local)
$ ls
aa.txt  LICENSE  README.en.md  README.md

JHP+Administrator@jhp MINGW64 /e/workspace-idea/testtag (testtag-v1-new-branch-local)
$ cat aa.txt
111111

切换分支后可以看到之前修改的内容并没有恢复,这是因为我们已经暂存了,接下来弹出暂存的内容:

$ git stash pop
On branch testtag-v1-new-branch-local
Your branch is up to date with 'origin/testtag-v1-new-branch'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   "\346\273\264\346\273\264\346\273\264.txt"

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   aa.txt

Dropped refs/stash@{0} (1fee21c566eb75f8c695247ffe691c8c68bf5c9d)

然后在查看,修改的内容就回来了:

$ ls
aa.txt  LICENSE  README.en.md  README.md  滴滴滴.txt

JHP+Administrator@jhp MINGW64 /e/workspace-idea/testtag (testtag-v1-new-branch-local)
$ cat aa.txt
111111
巴拉巴拉喇叭喇叭

JHP+Administrator@jhp MINGW64 /e/workspace-idea/testtag (testtag-v1-new-branch-local)
$ cat 滴滴滴.txt
滴滴滴

接下来就可以继续愉快地写bug了。

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐