暂存stash

Git stash

介绍:

有时,正在开发进行到一部分的时候,比如有个紧急的bug需要处理,这时需要切换到另一个分支修复。 问题,你不想仅仅因为过会儿回到这一点而为做了一半的工作创建一次提交。 针对这个问题的答案是 git stash 命令。

git stash(暂存)会处理工作目录的脏的状态 ,修改的跟踪文件与暂存改动 - 然后将未完成的修改保存到一个栈上,而你可以在任何时候重新应用这些改动。

暂存

git stashgit stash save [--keep-index] [-u|--include-untracked] [--patch] [message] 暂存修改(把当前目录修改保存到暂存区)两个命令等价。
默认git stash只会暂存已经在索引的文件

  • message 暂存信息(不带默认自动添加信息)
  • --keep-index 不要暂存已经通过git add过的文件
  • --include-untracked或是-u 暂存任何创建未跟踪文件
  • --patch 交互式手动确认哪些修改需要暂存(应用在不需要全部暂存的场景)
$ git stash
Saved working directory and index state WIP on dev: 0ac2bea test fetch


$ git stash save 'test stash'
Saved working directory and index state On dev: test stash

# 修改b.py和c.py, git add b.py 
$ git add b.py
$ git stash save --keep-index 'test keep index'
Saved working directory and index state On dev: test keep index
$ git status
On branch dev
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

    modified:   b.py
    
# 新建d.py 修改b.py
$ git stash save -u 'test include untracked'
Saved working directory and index state On dev: test include untracked
$ git status 
On branch dev
nothing to commit, working tree clean

# 修改c.py
$ git stash --patch
diff --git a/c.py b/c.py
index e69de29..d98124f 100644
--- a/c.py
+++ b/c.py
@@ -0,0 +1 @@
+print 'patch'
Stash this hunk [y,n,q,a,d,/,e,?]? y

Saved working directory and index state WIP on dev: 0ac2bea test fetch

查看暂存

git list 查看暂存区列表

$ git stash list
stash@{0}: WIP on dev: 0ac2bea test fetch
stash@{1}: On dev: test stash

恢复暂存区

git stash apply [stash] 恢复暂出区修改到工作目录
git stash pop [stash] 恢复暂存区修改到工作目录 并删除暂存区对应暂存

  • stash 暂存信息标识(stash@{1} 不带默认是stash@{0})
# apply恢复
$ git stash apply
On branch dev
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

    modified:   b.py

no changes added to commit (use "git add" and/or "git commit -a")

# 查看暂存列表
$ git stash list
stash@{0}: WIP on dev: 0ac2bea test fetch
stash@{1}: On dev: test stash

# pop恢复
$  git stash pop
On branch dev
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

    modified:   b.py

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (61d62ac45e6608259d5f1ba9485319021b52f1b0)

# 查看暂存  stash@{1}已经被删除了
$ git stash list
stash@{0}: On dev: test stash

暂存区新建分支

当你暂存以后在当前的分支有了新的提交,如果恢复刚才的暂存区可能会有冲突,如果有冲突就不得不去解决冲突。如果在继续在暂存区处继续工作?可以运行git stash branch创建新的分支,检出存储区所在的提交,重新在那应用工作,然后在应用成功后扔掉储藏

git stash branch [stash] 从暂存区新建分支

  • stash 暂存信息标识(stash@{1} 不带默认是stash@{0})
$ git stash branch testchanges stash@{2}
Switched to a new branch 'testchanges'
d.py already exists, no checkout
Could not restore untracked files from stash

$ git branch
  dev
  foo
  master
* testchanges

你可能感兴趣的:(暂存stash)