By Ran Bar-Zik | 8/27/2018 | General |Beginners

Git - Stash

Git - Stash

In our last Git article, we looked at Git Alias which allows us to make useful shortcuts. In this article, I’ll explain how to use a nice little feature of Git that allows us to locally store our work—Git stash.

 

It’s most commonly used in teams or in large companies, but not only. Imagine a scenario in which you’re working comfortably on your branch and then something suddenly falls in your lap where you have to quickly switch to a different branch. You’d have a ton of changes which you still didn’t run commit on. What to do? You could run commit, but what if you still don’t want to because the changes still aren’t ready? What if you have processes that are connected to Git? For example, if when you use git commit, you run jshint. If the code is full of bugs, then you’re in trouble because you can’t run commit.

 

The solution is simple—stash. This command is a temporary saving of our files in the current branch. After running stash, we can easily move to a different branch, make changes and fix things, and then return to our branch with all the saved changes.

 

Sound complicated? Not at all. Let’s say I have changes to which I haven’t run commit on them yet. Running git stash will present me with the following text:

Saved working directory and index state WIP on master: d2f6507 First commit
HEAD is now at d2f6507 First commit

OK, what’s all that mean? Git is saving the changes and then returning the local files to the original version of the branch. Now I can move to a different branch without issue. And when I want to come back? Super easy:

$git stash list
stash@{0}: WIP on master: d2f6507 First commit

Here there will be of all of my stashes. If I want to return to the last, nothing could be easier. Simply run git stash apply:

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

       modified:   index.html

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

Now I’m back to the last one and all is well. If I want to go back to a specific stash, I just need to specify the name as it appears in the list. For instance:

git stash apply stash@{0}

I can create a stash with a slightly more normal name than the one automatically generated by Git:

C:\Users\barzik\test\examplegit>git stash save "My stash"
Saved working directory and index state On master: My stash
HEAD is now at d2f6507 First commit

Notice that the stash list is not erased even after I run apply. In order to erase the stash, we need to run git stash drop.

git stash drop stash@{0}

If you want to skip the erasing step and automatically erase directly after the apply, use the command pop. It does apply and then drop automatically.

 

If like me, you forget to run drop in your haste, you’ll start to acquire a little collection of stashes. You can then easily use the following command to clean up the entire stash list:

git stash clear

And that’s about it for the Git stash feature. Small, but super useful in Git. Till next time.

 

Previous article: Git Alias

Next article: Git SSH Keys

 

About the author: Ran Bar-Zik is an experienced web developer whose personal blog, Internet Israel, features articles and guides on Node.js, MongoDB, Git, SASS, jQuery, HTML 5, MySQL, and more. Translation of the original article by Aaron Raizen.

By Ran Bar-Zik | 8/27/2018 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now