GIT STASH

背景:使用git进行版本控制,我们往往使用分支(Branch)进行模块开发。当我们在某个分支上进行开发的时候,之前的一个稳定版本有一个BUG需要紧急修复,此时我们可以将本地提交,然后切换到相应的分支上去修改调试。但是如果你不想将未达到特定版本要求的本地代码提交去修改BUG,此时git stash可以帮上忙。

 

原理:git branch可以将未提交到本地和服务器的代码放入Git栈中,这个时候你的工作区间就和上次提交的内容一样了,然后你可以放心修改,等到BUG修复之后,再通过git stash apply将之前的工作应用回来。

 

用法:1、stash当前修改

git stash 会把所有未提交的修改都保存起来,包括暂存和非暂存的,用于后续回复当前工作目录。

$ git status
On branch master
Changes to be committed:

new file:   style.css

Changes not staged for commit:

modified:   index.html

$ git stash
Saved working directory and index state WIP on master: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepage

$ git status
On branch master
nothing to commit, working tree clean

stash不会通过git push命令上传到git server上,一般我们使用git stash save "message"记录版本。

$ git stash save "test-cmd-stash"
Saved working directory and index state On autoswitch: test-cmd-stash
HEAD 现在位于 296e8d4 remove unnecessary postion reset in onResume function
$ git stash list
stash@{0}: On autoswitch: test-cmd-stash

2、重新应用缓存的stash

通过git stash pop命令回复之前缓存的工作目录此时缓存堆栈中的第一个stash删除,并将对应的修改应用到当前目录下。

$ git status
On branch master
nothing to commit, working tree clean
$ git stash pop
On branch master
Changes to be committed:

    new file:   style.css

Changes not staged for commit:

    modified:   index.html

Dropped refs/stash@{0} (32b3aa1d185dfe6d57b3c3cc3b32cbf3e380cc6a)

如果你不想删除stash中的此次的内容,可以使用git stash apply命令

$ git stash apply
On branch master
Changes to be committed:

    new file:   style.css

Changes not staged for commit:

    modified:   index.html

3、查看现有的stash,可以使用git stash list命令,如果想要切换到指定的stash,可以使用git stash apply 命令stash@{对应标号},默认使用0.

$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log

4、使用git stash drop命令移除

$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log
$ git stash drop stash@{0}
Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)

5、查看指定的stash的diff

$ git stash show -p
diff --git a/style.css b/style.css
new file mode 100644
index 0000000..d92368b
--- /dev/null
+++ b/style.css
@@ -0,0 +1,3 @@
+* {
+  text-decoration: blink;
+}
diff --git a/index.html b/index.html
index 9daeafb..ebdcbd2 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,2 @@
+<link rel="stylesheet" href="style.css"/>

6、从stash创建分支

如果你储藏了一些工作,暂时不去理会,然后继续在你储藏工作的分支上工作,你在重新应用工作时可能会碰到一些问题。如果尝试应用的变更是针对一个你那之后修改过的文件,你会碰到一个归并冲突并且必须去化解它。如果你想用更方便的方法来重新检验你储藏的变更,你可以运行 git stash branch,这会创建一个新的分支,检出你储藏工作时的所处的提交,重新应用你的工作,如果成功,将会丢弃储藏。

$ git stash branch testchanges
Switched to a new branch "testchanges"
# On branch testchanges
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#      modified:   index.html
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#
#      modified:   lib/simplegit.rb
#
Dropped refs/stash@{0} (f0dfc4d5dc332d1cee34a634182e168c4efc3359)

7、暂存未跟踪或忽略的文件

默认git stash会缓存下列文件:

  1. 添加到暂存区的修改
  2. Git跟踪的但未添加到暂存区的修改

但不会缓存工作目录中的新文件和被忽略的文件。

git stash命令提供了参数用于缓存上面两种类型的文件。使用-u或者--include-untracked可以stash untracked文件。使用-a或者--all命令可以stash当前目录下的所有修改。

 

文章来源:https://www.cnblogs.com/tocy/p/git-stash-reference.html

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值