Git如何stash部分文件
今天工作的時候有這樣一個訴求,改了本地大量的代碼,但是有兩個文件是適配本地的配置文件不需要上庫,如果git commit [filename]
的話需要寫很多文件,很不方便,于是使用了stash方法,這里做個記錄.
首先解釋下git stash
的作用,git stash
是將本地沒有commit的部份全部存儲起來,這樣方便你進行pull之類的操作,具體可以參考Git 工具 - 儲藏與清理.
但是如果直接git stash的話,會將當前所有文件都存儲起來,而我只想存儲兩個配置文件,其他的全部一起commit,這應該怎么辦呢?這里需要用到一個git stash -p
的命令;它是一個交互式命令,我們可以一個文件一個文件的遍歷,決定每個文件的操作方式.
root /u/c/s/cbs (master)# git stash -p
diff --git a/cmd/scripts/cbs.sh b/cmd/scripts/cbs.sh
old mode 100644
new mode 100755
Stash mode change [y,n,q,a,d,/,?]?
這里的[y,n,q,a,d,/,?]
分別代表的含義如下:
y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk nor any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk nor any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
所以,遇到我們需要stash的文件,我們就y,不需要stash需要commit的文件,我們就n,如果接下來沒有需要stash的文件,則直接q退出就行.
將文件保存好后,我們就可以commit和push剩下的代碼了.
git commit -m ""
git push origin master
然后我們將stash的文件恢復到本地,所有的操作就完成了.
git stash pop
如果大家有更好的方法解決我遇到的問題,也可以和我交流.