1. 程式人生 > >git stash 命令

git stash 命令

默認 同時 sea rop 是我 指定 使用 世界 mes

摘自: http://blog.csdn.net/longxiaowu/article/details/26815433

關於git stash命令的使用方法網上一大把,我想記錄的是我在使用過程中覺得實用及好用的:

當在一個分支的開發工作未完成,卻又要切換到另外一個分支進行開發的時候,除了commit原分支的代碼改動的方法外,我覺得git stash是一個更加便捷的選擇。

步驟如下:

一、添加改動到stash。在原分支 git stash save -a "messeag",網上很多很多資料都沒有加 -a 這個option選項,我想他們的代碼開發可能都是在原代碼上進行修改吧。而對於在項目裏加入了代碼新文件的開發來說,-a選項才會將新加入的代碼文件同時放入暫存區。

二、恢復改動。如果你要恢復的是最近的一次改動,git stash pop即可,我用這個用的最多。如果有多次stash操作,那就通過git stash list查看stash列表,從中選擇你想要pop的stash,運行命令git stash pop stash@{id}或者 git stash apply stash@{id}即可。這方面網上的資料挺多的。

三、刪除stashgit stash drop <stash@{id}> 如果不加stash編號,默認的就是刪除最新的,也就是編號為0的那個,加編號就是刪除指定編號的stash。git stash clear 是清除所有stash,整個世界一下子清凈了!

四、git stash pop git stash apply <stash@{id}> 的區別。

當我使用git stash pop git stash apply 幾次以後,我發現stash list 好像比我預計的多了幾個stash。於是我便上網去了解了一下這兩個命令的區別。原來git stash pop stash@{id}命令會在執行後將對應的stash id 從stash list裏刪除,而 git stash apply stash@{id} 命令則會繼續保存stash id。對於有點強迫癥的我來說,是容不下越來越多的陳舊stash id 仍然存在的,所以我更習慣於用git stash pop

命令。

git stash 命令