180608-Git工具之Stash
阿新 • • 發佈:2018-06-08
-m https stage images not sele def 建議 本地
git stash 暫存
背景:
實際開發過程中,經常可能遇到的一個問題,當你在dev分支上正開發得happy的時候;突然來了個線上bug,得趕緊從release分支上切一個bugfix分支來解決線上問題,這個時候,正在開發的東西,就得暫存了
之前對於Git只是簡單的了解了下,只處於入門的會用級別,遇到上面這個問題,采取的是一個比較笨的方案:
# 1. 將當前改動保存,並提交一個tmp commit
git add .
git commit -m ‘tmp save‘
## 註意上面只是提交到本地,沒有推送到遠端倉庫
# 2. 開始bugfix
## 然後切換到release 分支,並獲取最新代碼
git checkout release
git pull origin release
## 新建bugfix分支
git checkout -b bugfix
... # 開始幹活
# 3. 回到原來分支,繼續幹活
git checkout dev
git log
git reset --soft commit號
雖然上面這樣也可以曲線救國,但是在了解到Git stash之後,就簡單多了,不需要commit和reset
I. git stash 命令
1. 基本命令
將當前改動暫存,恢復到上一次commit號對應的狀態
git stash
上面執行完畢之後,當前所有的改動會被暫存,然後工作區變得幹凈,使用git status
會發現沒有修改
查看暫存的列表
git stash list
如果需要恢復之前的改動,執行
git stash pop
2. 實例演示
下面是個人的一個項目的測試結果
? git status
On branch master
Your branch is up-to-date with ‘origin/master‘.
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: spring-rabbit/src/test/java/com/git/hui/rabbit/spring/SprintUnit.java
modified: spring-rabbit/src/test/java/com/git/hui/rabbit/spring/fac/FacMQConsumer.java
no changes added to commit (use "git add" and/or "git commit -a")
? git stash
Saved working directory and index state WIP on master: 8a96c7b 添加基於配置的消費者方式
HEAD is now at 8a96c7b 添加基於配置的消費者方式
? git stash list
stash@{0}: WIP on master: 8a96c7b 添加基於配置的消費者方式
? git status
On branch master
Your branch is up-to-date with ‘origin/master‘.
nothing to commit, working tree clean
? git stash pop
On branch master
Your branch is up-to-date with ‘origin/master‘.
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: spring-rabbit/src/test/java/com/git/hui/rabbit/spring/SprintUnit.java
modified: spring-rabbit/src/test/java/com/git/hui/rabbit/spring/fac/FacMQConsumer.java
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (fa73ca947d591003cd46a49f6d657cce43756d1a)
? git stash list
? git status
On branch master
Your branch is up-to-date with ‘origin/master‘.
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: spring-rabbit/src/test/java/com/git/hui/rabbit/spring/SprintUnit.java
modified: spring-rabbit/src/test/java/com/git/hui/rabbit/spring/fac/FacMQConsumer.java
no changes added to commit (use "git add" and/or "git commit -a")
3. 進階
簡單的使用場景下,需要暫存時,直接輸入 git stash
命令即可;需要恢復執行 git stash pop
;
如果出現多次的工作區暫存,會怎樣?
兩次暫存之後: (最近暫存的,序號最小)
? git stash list
stash@{0}: WIP on master: 8a96c7b 添加基於配置的消費者方式
stash@{1}: WIP on master: 8a96c7b 添加基於配置的消費者方式
應用某次暫存:
git stash apply stash@{1}
執行apply只是會恢復當時暫存的內容,但是不會刪除,如果需要刪除指定stash,可以執行
git stash drop stash@{1}
4. 取消儲藏
在某些情況下,你可能想應用儲藏的修改,在進行了一些其他的修改後,又要取消之前所應用儲藏的修改。Git沒有提供類似於 stash unapply 的命令,但是可以通過取消該儲藏的補丁達到同樣的效果:
git stash show -p stash@{0} | git apply -R
II. 其他
1. 參考
- Git 工具 - 儲藏(Stashing)
2. 一灰灰Blog: https://liuyueyi.github.io/hexblog
一灰灰的個人博客,記錄所有學習和工作中的博文,歡迎大家前去逛逛
3. 聲明
盡信書則不如,已上內容,純屬一家之言,因個人能力有限,難免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激
- 微博地址: 小灰灰Blog
- QQ: 一灰灰/3302797840
4. 掃描關註
blogInfoV2.png180608-Git工具之Stash