Git stash usecase

Tagged as:

So someone asked in our Telegram group about git stash. Stash definitely is one of my favorite command in git. Like it so much that while I was still working with svn, I looked for an svn equivalent, svn stash !

Stash is great when you need to keep aside first what you’re working right now, without having to make it into the history. This is the difference with branch, as someone might ask - why don’t just use a branch ? With branch, you have to commit the stuff and it permanently recorded in the history, unless you use something like rebase later on for merging.

One of the question in the discussion was, when running git stash, what happen to the file already added to the index but not yet committed ? So I looked into the manual and answered this:-

Then I did some test:-

> git diff
diff --git a/README.txt b/README.txt
index ce01362..94954ab 100644
--- a/README.txt
+++ b/README.txt
@@ -1 +1,2 @@
 hello
 +world
 kamal@Kamal-no-MacBook-Air~/test-stash
 > git add README.txt
 kamal@Kamal-no-MacBook-Air~/test-stash
 > git diff --cached
 diff --git a/README.txt b/README.txt
 index ce01362..94954ab 100644
 --- a/README.txt
 +++ b/README.txt
 @@ -1 +1,2 @@
  hello
  +world
  kamal@Kamal-no-MacBook-Air~/test-stash
  > git stash
  Saved working directory and index state WIP on master: 5ae203d initial
  HEAD is now at 5ae203d initial
  kamal@Kamal-no-MacBook-Air~/test-stash
  > git diff --cached
  kamal@Kamal-no-MacBook-Air~/test-stash
  >

git stash --keep-index - so yang dah add stay in index, when the guy come back he can commit that.

> git add README.txt
kamal@Kamal-no-MacBook-Air~/test-stash
> git stash --keep-index
Saved working directory and index state WIP on master: 5ae203d initial
HEAD is now at 5ae203d initial
kamal@Kamal-no-MacBook-Air~/test-stash
> git diff --cached
diff --git a/README.txt b/README.txt
index ce01362..94954ab 100644
--- a/README.txt
+++ b/README.txt
@@ -1 +1,2 @@
 hello
 +world

Notice the changes still in index after stash.

Should you use stash as backup ?

Definitely no, shouldn’t use stash as backup, that definitely an abuse of the feature.

Rule of thumb - stash should always been used in single session. If you need to get back at it later - use branch.

Written on March 11, 2016 by kamal
About author

Lead Engineer at Xoxzo Inc. Develop web applications in Python/Django. Interested in open source and tech talent development.

Xoxzo.com provide API services for developer to develop messaging and voice based application. The postings on this site are my own and don't necessarily represent my employer's positions, strategies or opinions. I can be reached via Twitter @k4ml.

Ikuti perkembangan terkini melalui twitter kami @koditi atau FB Page fb.me/koditi.