文档章节

How to Git Stash Your Work in the Correct Way

Kolosek
 Kolosek
发布于 2018/05/15 16:45
字数 692
阅读 2
Git">收藏 0

Imagine that you are working on a part of a project and it starts getting messy. There has been an urgent bug that needs your immediate attention. It is time to save your changes and switch branches. The problem is, you don’t want to do a commit of half-done work. The solution is git stash.

Stashing is handy if you need to quickly switch context and work on something else but you're mid-way through a code change and aren't quite ready to commit. By Bitbucket

Stashing

Let's say you currently have a couple of local modifications. Run git status, to check your current state:

$ git status
# On branch master
# 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:   assets/stylesheets/styles.css

We need to work on that urgent bug. First, we want to save out unfinished work changes without committing them. This is where git stash comes as a savior:

$ git stash
Saved working directory and index state WIP on master:
  bb06da6 Modified the index page
HEAD is now at bb06da6 Modified the index page
(To restore them type "git stash apply")

Your working directory is now clean and all uncommitted local changes have been saved! At this point, you're free to make new changes, create new commitsswitch branches, and perform any other Git operations.

By default, stashes are identified as "WIP" – work in progress, on top of the branch and commit they are created from.

Re-applying Your Stash

Git stash is a temporary storage. When you're ready to continue where you left off, you can restore the saved state easily: git stash pop.

Popping your stash removes the changes from your stash and reapplies the last saved state. If you want to keep the changes in the stash as well, you can use git stash apply instead.

Additional Tips and Tricks

There are a couple of other things you can do with a stash. Let's take a look!

  • Saving stashes
    Save a stash with a message$ git stash save <message>.

Try this out by adding CSS-line high to your styles and stash it with a nice comment.

  • Stashing untracked files
    This is the only way to save untracked files$ git stash -u or $ git stash --include-untracked
  • List multiple stashes
    When you git stash or git stash save, Git will create a Git commit object with a name and then save it in your repo. You can view the list of stashes you made at any time! $ git stash list.
$ git stash list
stash@{0}: On master: Modified the index page
stash@{1}: WIP on master: bb06da6 Initial Commit
  • Partial stashes
    You can choose to stash just a single file, a collection of files, or individual changes from within files: $ git stash -p or $ git stash --patch.

RSpec tests are a must in the Ruby on Rails projects, but they might not be always complete. Stash only the part that is ready to go!

  • Viewing stash diffs
    There are two ways to view a stash: to view the full diff of a stash - $ git stash show -p or view only the latest stash - $ git stash show.
$ git stash show
index.html | 1 +
style.css | 2 ++
2 files changed, 3 insertions(+)
  • Creating a branch from the stash
    Create a new branch to apply your stashed changes to, and then pop your stashed changes onto it: $ git stash branch <branch_name> <stash_id>.

This is another way to save your stash before moving on with the project.

  • Remove your stash
    Use it with caution, it maybe is difficult to revert. The only way to revert it is if you didn't close the terminal after deleting the stash.
    If you no longer need a particular stash, you can delete it with: $ git stash drop <stash_id>. Or you can delete all of your stashes from the repo with: $ git stash clear.

Hope this article helped you to get a better understanding how stashing works. Be sure to test it out!

This article is originally published on Kolosek Blog.

© 著作权归作者所有

Kolosek
粉丝 0
博文 29
码字总数 20461
作品 0
塞尔维亚
CEO
私信 提问
git stash和git stash pop

git stash 可用来暂存当前正在进行的工作, 比如想pull 最新代码, 又不想加新commit, 或者另外一种情况,为了fix 一个紧急的bug, 先stash, 使返回到自己上一个commit, 改完bug之后再stash...

JamesView
2018/02/01
23
0
《Git权威指南》读书笔记 第九章 恢复进度

9.1 恢复保存的进度 查看第五章保存的进度: 恢复进度: 查看工作区状态,进度已经找回了: 对找回进度的工作区做以下处理: (1)对当前的暂存区进行提交: (2)撤销之前的提交,工作区和暂...

一万
2016/07/18
59
0
Git随手查,10分钟搞定git命令

1.$ git config --global user.name "Your Name" $ git config --global user.email "email@example.com" 2.初始化一个Git仓库,使用git init命令。 3.添加文件到Git仓库,分两步: 第一步,......

大道至精
2016/08/10
106
0
每日一剂—mysql事务锁 & git reflog

查找mysql事务锁 查看事务和锁的信息 explain内容解析 select_type:SIMPLE,这是一个简单类型的SQL语句,不含子查询或者UNION。 type:index,访问类型,即找到所需数据使用的遍历方式,潜在...

阿dai学长
07/02
32
0
Git 2.7.0 RC3 发布

Git 2.7.0 RC3 发布,此版本主要改进: * The appearance of "gitk", particularly on high DPI monitors, have been improved. "gitk" also comes with an undated translation for Swedish......

oschina
2015/12/29
1K
2

没有更多内容

加载失败,请刷新页面

加载更多

nginx学习笔记

中间件位于客户机/ 服务器的操作系统之上,管理计算机资源和网络通讯。 是连接两个独立应用程序或独立系统的软件。 web请求通过中间件可以直接调用操作系统,也可以经过中间件把请求分发到多...

码农实战
55分钟前
5
0
Spring Security 实战干货:玩转自定义登录

1. 前言 前面的关于 Spring Security 相关的文章只是一个预热。为了接下来更好的实战,如果你错过了请从 Spring Security 实战系列 开始。安全访问的第一步就是认证(Authentication),认证...

码农小胖哥
今天
8
0
JAVA 实现雪花算法生成唯一订单号工具类

import lombok.SneakyThrows;import lombok.extern.slf4j.Slf4j;import java.util.Calendar;/** * Default distributed primary key generator. * * <p> * Use snowflake......

huangkejie
昨天
11
0
PhotoShop 色调:RGB/CMYK 颜色模式

一·、 RGB : 三原色:红绿蓝 1.通道:通道中的红绿蓝通道分别对应的是红绿蓝三种原色(RGB)的显示范围 1.差值模式能模拟三种原色叠加之后的效果 2.添加-颜色曲线:调整图像RGB颜色----R色增强...

东方墨天
昨天
10
1
将博客搬至CSDN

将博客搬至CSDN

算法与编程之美
昨天
12
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部