首页 > 代码库 > git stash封存分支 以及关于开发新功能的处理

git stash封存分支 以及关于开发新功能的处理

有种情况,我们要修复项目的bug时,但别的分支有修改的代码,要修复的bug可能会影响(所有分支共用一个暂存区)。可以单独创建一个bug分支,用于修复和提交bug,在修改前可以先stash封存分支修改的代码。

测试,首先在slave分支里修改文件:456的内容,然后执行git stash 封存slave分支未提交的代码。

技术分享
laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git checkout slave
Switched to branch slave

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git status
On branch slave
nothing to commit, working tree clean

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ ls
456  998  bb.css  index.html  info.py  new

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ cat 456

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ vim 456

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ cat 456
修改456文件

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git stash
warning: LF will be replaced by CRLF in 456.
The file will have its original line endings in your working directory.
Saved working directory and index state WIP on slave: 6957dae add 998
HEAD is now at 6957dae add 998

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git stash
No local changes to save

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git log --oneling
fatal: unrecognized argument: --oneling

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git log --oneline
6957dae add 998
775f3ab add new
6abf028 modify info.py
a068c80 add info.py
c5b475a Revert "add 123"
cfcbd5c add 456
13f5bcb add 123
8110523 Revert "add adc"
74f7cb6 add bb.css
577fab6 Revert "revert abc"
e1f2701 add adc
358cdac 添加UI.js
04c94a8 添加一个文件index.html
git stash

创建bug01分支,并修复和提交bug文件:998,然后切换到master分支,合并bug01分支

技术分享
$ git branch bug01

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git branch
  bug01
  master
* slave

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git checkout bug01
Switched to branch bug01

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (bug01)
$ vim 998

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (bug01)
$ cat 998
fsaldkfsalfjlasjfl
修复bug内容部分

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (bug01)
$ git status
On branch bug01
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:   998

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

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (bug01)
$ git add 998

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (bug01)
$ git commit -m "monify bug 998"
[bug01 487e2eb] monify bug 998
 1 file changed, 1 insertion(+)

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (bug01)
$ git checkout master
Switched to branch master

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git merge
merge       mergetool

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git merge bug01 --no-ff -m "merge bug01"
Merge made by the recursive strategy.
 998 | 1 +
 1 file changed, 1 insertion(+)

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git log --oneline
f698663 merge bug01
487e2eb monify bug 998
85bd6ea add dd.css
bdba943 merge slave --no--ff
6957dae add 998
112da02 modify new
775f3ab add new
8b5d2fd add new
6abf028 modify info.py
a068c80 add info.py
c5b475a Revert "add 123"
cfcbd5c add 456
13f5bcb add 123
8110523 Revert "add adc"
74f7cb6 add bb.css
577fab6 Revert "revert abc"
e1f2701 add adc
358cdac 添加UI.js
04c94a8 添加一个文件index.html
git merge

最后再解封slave,使用git stash pop可以解封最近的一次封存,使用git stash apply 封存编号可以解封指定的封存位置。

技术分享
$ git checkout slave
Switched to branch slave

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git status
On branch slave
nothing to commit, working tree clean

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git stash list
stash@{0}: WIP on slave: 6957dae add 998

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git stash pop
On branch slave
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:   456

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

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git stash
Saved working directory and index state WIP on slave: 6957dae add 998
HEAD is now at 6957dae add 998

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git stash list
stash@{0}: WIP on slave: 6957dae add 998

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git stash apply stash@{0}
On branch slave
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:   456

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

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$
git stash pop

 

git stash封存分支 以及关于开发新功能的处理