Git 分支管理-git stash 和git stash pop


https://blog.csdn.net/u010697394/article/details/56484492

合並分支,沖突是難免的,在實際協作開發中我們遇到的情況錯綜復雜,今天就講兩個比較重要的命令使用git stash和git stash pop

試想一下:1.假如我們在develop分支開發,這時候突然技術經理說有個緊急修復下,這修復bug之前說了,需要從master穩定版本開一個分支,而我們develop如果沒有commit,而直接切換到master,會有如下提示:

 

  1.  
    zxdeMacBook-Pro:hswallpager zs$ git checkout master
  2.  
    error: Your local changes to the following files would be overwritten by checkout:
  3.  
    app/src/main/java/Activity.java
  4.  
    Please, commit your changes or stash them before you can switch branches.
  5.  
    Aborting
  6.  
     
  7.  
    zxdeMacBook-Pro:hswallpager zs$ git branch
  8.  
    * develop
  9.  
    master


根據提示,我們需要提交修改或者在切換分之前 stash 一下。而我們每次間斷就要commit一次,將來git log里會有很多臨時提交,太多了讓人無法快速定位,而這的確不是我們想要的,那就只有stash。stash的含義就是把工作區的修改臨時儲藏起來,等以后再恢復使用。那我們不妨一試git stash,看看結局是什么樣子的:

 

 

  1.  
    zxdeMacBook-Pro:hswallpager zs$ git stash
  2.  
    Saved working directory and index state WIP on develop: b70f2af develop update
  3.  
    HEAD is now at b70f2af develop update

 

 

先看最后一句 "HEAD is now at b70f2af develop update". 還記得上一篇的分支圖吧。

 

  1.  
    * 073fc5c 合並后的修改
  2.  
    |\
  3.  
    | * b70f2af develop update
  4.  
    * | 41754e3 修改
  5.  
    |/

 

 

因為我們上一篇master合並后develop后,並沒有將develop的分支和master同步,因此develop分支的最新的提交記錄就在b70f2af,也就是工作區目前是干凈的。git stash  執行后,develop分支就相當於什么也沒操作一樣。

 

接着我們在執行最開始的切換到master分支,看看會怎樣,還會不會提示上述信息:

 

  1.  
    zxdeMacBook-Pro:hswallpager zs$ git checkout master
  2.  
    Switched to branch 'master'
  3.  
    Your branch is ahead of 'origin/master' by 8 commits.
  4.  
    (use "git push" to publish your local commits)
  5.  
     
  6.  
    zxdeMacBook-Pro:hswallpager zs$ git branch
  7.  
    develop
  8.  
    * master


怎么樣,是不是正確切換到master分支了。現在我們在新建並切換分支hotfixes-01.然后可以修復緊急bug了。然后修改,提交,刪除hotfixes-01即可。然后我們繼續切回develop分支:

 

 

  1.  
    zxdeMacBook-Pro:hswallpager zs$ git checkout develop
  2.  
    Switched to branch 'develop'
  3.  
    zxdeMacBook-Pro:hswallpager zs$ git status
  4.  
    On branch develop
  5.  
     
  6.  
    nothing to commit, working directory clean

 

 

這時候我們可以先把master分支的修改合並到develop,操作步驟以前也學過了,合並沖突等。這時候我們看會代碼,stash之前的代碼已經看不到了。那我們怎么繼續接着上述的修改恢復現場呢。這時候用到git stash pop。
我們先看一下stash清單,執行git stash list。

 

  1.  
    zxdeMacBook-Pro:hswallpager zs$ git stash list
  2.  
    stash@{ 0}: WIP on develop: b70f2af develop update


然后我們用git stash pop 恢復現場,看一下結果:

 

 

  1.  
    zxdeMacBook-Pro:hswallpager zs$ git stash pop
  2.  
    On branch develop
  3.  
     
  4.  
    Changes not staged for commit:
  5.  
    ( use "git add <file>..." to update what will be committed)
  6.  
    ( use "git checkout -- <file>..." to discard changes in working directory)
  7.  
     
  8.  
    modified: app/src/ main/java/Activity.java
  9.  
     
  10.  
    no changes added to commit (use "git add" and/or "git commit -a")
  11.  
    Dropped refs/stash@{ 0} (44c79bddb5c6c3848bc0de0b687cf14d4907b012)

 

 

這時候在看工作區的源代碼,發現已經正確恢復現場,可以繼續在以前基礎上工作了。

 

2.現在另一種情況:你pull最新代碼,但這時候你又不想重新增加commit記錄,這時候先git stash,然后pull,最后在git stash pop,

 

這1和2兩種情況在實際開發過程中會經常用到,要熟練掌握git stash的應用。

補充:在我們多次使用git stash 后,git棧里充滿了很多未提交的代碼,這時候用git stash list 可以講git 棧信息打印出來,比如

git stash apply stash@{1} 就代表把指定版本號為stash@{1}的工作取出來。清空的話使用git stash clear。

 

git stash pop 和 git stash apply 的不同:

apply 讀取暫存區的數據,通過apply后,暫存區的數據依然存在。

pop 是取出最新的一次暫存數據,pop后,暫存區就不會存在這次數據了。

 

總結:

git stash  #可用來暫存當前正在進行中的工作

git stash pop  #從git棧中恢復第一個。相當於git stash apply 和git stash drop

git stash list   #打印git棧中的所有信息

git stash clear  #清空git棧

git stash apply stash@{1}  #將你指定版本號為stash@{1}的工作取出

 

版本分支是git區分集中式版本控制的一大優勢,而為了保證團隊協作中順利的開發,建議大家多用分支,至於具體分支的命名,不必拘泥死板,根據自己團隊的實際情況,讓分支成為我們團隊開發的助推器,而不是拖后腿。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM