Git学习 - Git stash 的用法

Git学习 - Git stash 的用法

1、引言

有时候我们会遇到这样的情况:比如正在 dev 分支上开发一个新的需求,需求开发到了一半,这时有人反馈有 bug 急需要处理,但是你的新功能做到了一半还不想提交,这时就可以使用命令 git stash 把当前进度(工作区和暂存区)保存起来,然后切换到另外分支去修改 bug ,修改完成后再 git checkout dev 回到 dev 分支,使用命令 git stash pop 来恢复之前的进度进行继续开发;

2、如何使用

1、先来介绍两个命令: git stashgit stash save ""

git stash: 将工作区和暂存区保存起来

git stash save “case”: 给本次存储起了个名字"case",便于查找。

2、git stash list:

查看之前存储的所有版本列表

$ git stash list
stash@{0}: WIP on Practice: 2f70846 Complete practice view and network
stash@{1}: WIP on Practice: 2f70846 Complete practice view and network
stash@{2}: WIP on Practice: 2f70846 Complete practice view and network
stash@{3}: WIP on Practice: 2f70846 Complete practice view and network
stash@{4}: WIP on Practice: 812e77b Add collectionView and AnswerView
stash@{5}: WIP on Practice: 53bb0c1 add tableView of questions and refactor the code of scrollView

3、git stash pop [stash_id]git stash apply

git stash pop: 恢复具体某一次的版本,如果不指定 stash_id 的话则默认恢复最新的存储版本,恢复成功后输入命令:git status 就可以看到你所修改过的文件.

$ git stash pop stash@{0}
Auto-merging WePeiYang/Shared/Network/SolaSessionManager.swift
CONFLICT (content): Merge conflict in WePeiYang/Shared/Network/SolaSessionManager.swift
Auto-merging WePeiYang/Practice/Practice/QuestionTableView/OptionsCell.swift
CONFLICT (content): Merge conflict in WePeiYang/Practice/Practice/QuestionTableView/OptionsCell.swift
Auto-merging WePeiYang/Practice/Practice/Exercise/Model/ExerciseNetwork.swift
Auto-merging WePeiYang.xcodeproj/project.pbxproj
CONFLICT (content): Merge conflict in WePeiYang.xcodeproj/project.pbxproj

git stash apply: 将堆栈中的内容应用到当前目录,不同于git stash pop,该命令不会将内容从堆栈中删除,也就说该命令能够将堆栈的内容多次应用到工作目录中,适应于多个分支的情况。

$ git stash apply
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   src/main/java/com/wy/StringTest.java

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

$ git stash list
stash@{0}: On master: case2
stash@{1}: On master: case1

4、git stash drop [stash_id]

删除一个存储的进度。如果不指定stash_id,则默认删除最新的存储进度。

$ git stash drop stash@{5}
Dropped stash@{5} (00a18888b0d4c7e9c7d543e9798e7de8df967bc3)

5、git stash clear

清除所有的存储进度

6、git stash show [stash_id]

如果 stash_id 不指定的话则默认查看堆栈中最新保存的stash和当前目录的差异。

git stash show stash@{1} 查看指定的stash和当前目录差异 。

通过 git stash show -p 查看详细的不同:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值