• git stash 操作


    git stash 操作

    git stash 是一个还挺方便,但是不会也不太要紧的指令,主要的应用场景为当部分的代码被修改了,但是又不想要 commit 当前的变化,便可以使用 git stash 将所有的变化进行保存,保持 working tree 干净。使用 git stash 会还原到上一个 commit 的状态。想要获取 stashed 的文件,就可以使用 git stash pop

    换言之,使用 git commit + git revert --soft HEAD~1 的组合技也一样,就是多打了几个字会稍微麻烦一些。

    stash list

    可以用来显示所有的 stash 记录

    stash

    没什么卵用的补充 git stash 的完整指令是 git shtash store

    大多数情况下来说,使用 git stash + git stash pop 就够了,不过 git stash 是一个栈(通过 pop 推测的),自然也可以存储多个 stash,查看 stashed 的记录可以使用 git stash list,如:

    ➜  stashing git:(main) git stash
    Saved working directory and index state WIP on main: 2ea1a40 initial commit
    ➜  stashing git:(main) git stash
    Saved working directory and index state WIP on main: 2ea1a40 initial commit
    ➜  stashing git:(main) git stash
    Saved working directory and index state WIP on main: 2ea1a40 initial commit
    ➜  stashing git:(main)git stash -m "another lorem"
    Saved working directory and index state On main: another lorem
    ➜  stashing git:(main) git stash list
    ➜  stashing git:(main) git stash list
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    stash@{0}: On main: another lorem
    stash@{1}: WIP on main: 2ea1a40 initial commit
    stash@{2}: WIP on main: 2ea1a40 initial commit
    stash@{3}: WIP on main: 2ea1a40 initial commit
    (END)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    友情提示,可以使用 -m 这个 flag 添加一些信息,这样之后回来看 stashed 的变化也知道做了些什么。

    stash pop

    直接 stash pop 会进行退栈操作,如:

    ➜  stashing git:(main) git stash list
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    stash@{0}: On main: pop first
    stash@{1}: On main: stash example
    stash@{2}: On main: another lorem
    stash@{3}: WIP on main: 2ea1a40 initial commit
    stash@{4}: WIP on main: 2ea1a40 initial commit
    stash@{5}: WIP on main: 2ea1a40 initial commit
    (END)
    ➜  stashing git:(main) git stash pop
    On branch main
    Changes not staged for commit:
      (use "git add ..." to update what will be committed)
      (use "git restore ..." to discard changes in working directory)
    	modified:   index.html
    
    no changes added to commit (use "git add" and/or "git commit -a")
    Dropped refs/stash@{0} (780899dc334d6cd257fef560389fce018d314c08)
    ➜  stashing git:(main)git stash list
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    stash@{0}: On main: stash example
    stash@{1}: On main: another lorem
    stash@{2}: WIP on main: 2ea1a40 initial commit
    stash@{3}: WIP on main: 2ea1a40 initial commit
    stash@{4}: WIP on main: 2ea1a40 initial commit
    (END)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    stash apply

    使用 git stash pop 也可以选定指定的下标进行退栈操作,顺便,可以直接使用 index 而不需要输入完整的 stash@{index},如:

    ➜  stashing git:(main) git stash list
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    stash@{0}: On main: revert current change to avoid merge conflict
    stash@{1}: On main: stash example
    stash@{2}: On main: another lorem
    stash@{3}: WIP on main: 2ea1a40 initial commit
    stash@{4}: WIP on main: 2ea1a40 initial commit
    stash@{5}: WIP on main: 2ea1a40 initial commit
    (END)
    ➜  stashing git:(main) git stash pop 1
    On branch main
    Changes not staged for commit:
      (use "git add ..." to update what will be committed)
      (use "git restore ..." to discard changes in working directory)
    	modified:   index.html
    
    no changes added to commit (use "git add" and/or "git commit -a")
    Dropped refs/stash@{1} (19fbf2dd8719108de5d3d904a5752fe47e0f362c)
    ➜  stashing git:(main)git stash list
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    stash@{0}: On main: revert current change to avoid merge conflict
    stash@{1}: On main: another lorem
    stash@{2}: WIP on main: 2ea1a40 initial commit
    stash@{3}: WIP on main: 2ea1a40 initial commit
    stash@{4}: WIP on main: 2ea1a40 initial commit
    (END)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    可以看到,栈顶 stashed 的文件没有被弹出,反而是 index 为 1 的对应 stashed 文件被弹出。

    stash apply

    git stash apply 指令与将对应 stashed 文件应用于当前分支上,但是并不会进行退栈操作,如果某一条 stashed 的记录需要确认在多条分支上都需要工作时,就可以使用这个指令,如:

    ➜  stashing git:(main) git stash list
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    stash@{0}: On main: stash changes
    stash@{1}: On main: revert current change to avoid merge conflict
    stash@{2}: On main: another lorem
    stash@{3}: WIP on main: 2ea1a40 initial commit
    stash@{4}: WIP on main: 2ea1a40 initial commit
    stash@{5}: WIP on main: 2ea1a40 initial commit
    (END)
    ➜  stashing git:(main) git stash apply stash@{2}
    On branch main
    Changes not staged for commit:
      (use "git add ..." to update what will be committed)
      (use "git restore ..." to discard changes in working directory)
    	modified:   index.html
    
    no changes added to commit (use "git add" and/or "git commit -a")
    ➜  stashing git:(main)git stash list
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    stash@{0}: On main: stash changes
    stash@{1}: On main: revert current change to avoid merge conflict
    stash@{2}: On main: another lorem
    stash@{3}: WIP on main: 2ea1a40 initial commit
    stash@{4}: WIP on main: 2ea1a40 initial commit
    stash@{5}: WIP on main: 2ea1a40 initial commit
    (END)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79

    stash drop

    git stash drop 用于删除对应 stashed 的变化,与 apply 对应,如:

    ➜  stashing git:(main)git stash list
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    stash@{0}: On main: stash changes
    stash@{1}: On main: revert current change to avoid merge conflict
    stash@{2}: On main: another lorem
    stash@{3}: WIP on main: 2ea1a40 initial commit
    stash@{4}: WIP on main: 2ea1a40 initial commit
    stash@{5}: WIP on main: 2ea1a40 initial commit
    (END)
    ➜  stashing git:(main)git stash drop 1
    Dropped refs/stash@{1} (1d6a06ed2ac0b17c6380a509224932c9137afd7e)
    ➜  stashing git:(main)git stash list
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    stash@{0}: On main: stash changes
    stash@{1}: On main: another lorem
    stash@{2}: WIP on main: 2ea1a40 initial commit
    stash@{3}: WIP on main: 2ea1a40 initial commit
    stash@{4}: WIP on main: 2ea1a40 initial commit
    (END)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    其他

    使用 stash 相关指令,尤其是应用变化(pop,apply)时可能会产生 merge conflict,这时候就比需要使用 commit 去解决 merge conflict。这也就是为什么说会用 stash 还挺方便的,不会的话新建 branch+commit+reset 也一样使用。毕竟需要修 merge conflict 的时候,总归都是要产生一个 commit 的。

  • 相关阅读:
    Node js 开发入门 —UDP 编程,小白也能轻松学会
    Hadoop作业篇(一)
    tcpdump相关
    在windows操作系统上安装MariaDB
    在Unity中模拟汽车的移动
    matplotlib入门之抛砖引玉
    Java面向对象(二)
    Docker从认识到实践再到底层原理(四-1)|Docker镜像仓库|超详细详解
    拯救者Legion Y9000K 2021H(82K6)原厂oem预装Win11系统镜像
    14-Linux之yum软件管理
  • 原文地址:https://blog.csdn.net/weixin_42938619/article/details/127980347