Git如何stash部分文件

作者: cbsfly_ | 来源:发表于2018-12-06 16:36 被阅读3次

    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
    

    如果大家有更好的方法解决我遇到的问题,也可以和我交流.

    相关文章

      网友评论

        本文标题:Git如何stash部分文件

        本文链接:https://www.haomeiwen.com/subject/njbocqtx.html