zoukankan      html  css  js  c++  java
  • 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区分集中式版本控制的一大优势,而为了保证团队协作中顺利的开发,建议大家多用分支,至于具体分支的命名,不必拘泥死板,根据自己团队的实际情况,让分支成为我们团队开发的助推器,而不是拖后腿。

  • 相关阅读:
    Python--json处理
    Python--加密模块
    Python--函数即变量
    bzoj 2276: [Poi2011]Temperature
    1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛
    2017 9 15 noip模拟赛
    bzoj 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛
    2017 9 11 noip模拟赛T2
    URAL 1613. For Fans of Statistics (2017 9 6 noip模拟赛T3)
    codeforces 105 B. Dark Assembly(birbe贿赂 noip模拟赛)
  • 原文地址:https://www.cnblogs.com/nafio/p/9627841.html
Copyright © 2011-2022 走看看