1. 程式人生 > >git stash封存分支 以及關於開發新功能的處理

git stash封存分支 以及關於開發新功能的處理

oca ash color 可能 top clean 代碼 desktop 單獨

有種情況,我們要修復項目的bug時,但別的分支有修改的代碼,要修復的bug可能會影響(所有分支共用一個暫存區)。可以單獨創建一個bug分支,用於修復和提交bug,在修改前可以先stash封存分支修改的代碼。

測試,首先在slave分支裏修改文件:456的內容,然後執行git stash 封存slave分支未提交的代碼。

技術分享
[email protected] MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git checkout slave
Switched to branch slave

[email protected]
/* */-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git status On branch slave nothing to commit, working tree clean [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ ls 456 998 bb.css index.html info.py new [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $
cat 456 [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ vim 456 [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ cat 456 修改456文件 [email protected]-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 [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git stash No local changes to save [email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave) $ git log --oneling fatal: unrecognized argument: --oneling [email protected]-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

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git branch
  bug01
  master
* slave

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git checkout bug01
Switched to branch bug01

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (bug01)
$ vim 998

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (bug01)
$ cat 998
fsaldkfsalfjlasjfl
修復bug內容部分

[email protected]-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")

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (bug01)
$ git add 998

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

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (bug01)
$ git checkout master
Switched to branch master

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git merge
merge       mergetool

[email protected]-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(+)

[email protected]-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

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$ git status
On branch slave
nothing to commit, working tree clean

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

[email protected]-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)

[email protected]-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

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

[email protected]-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")

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (slave)
$
git stash pop

git stash封存分支 以及關於開發新功能的處理