Using Git Stash

Git stash is a great way to maintain your current work and switch to another branch quickly. If you want to work on something else and aren’t ready to commit your half completed work, git stash allows you to switch context easily and then come back to your work later.

If you have some work that you haven’t committed yet, for example we adjusted our Makefile

➜  dev-diaries git:(master) ✗ gst
On branch master
Your branch is up to date with 'origin/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:   Makefile

We can stash that change and switch branches to work on something else:

> git stash

Then when we run a git status, we’ll see that there are no local changes

➜  dev-diaries git:(master) ✗ git stash                    
No local changes to save

If we then come back we can put those changes back in one of two ways. If we want keep that change around in our stash list we can just run git stash apply, or if we don’t want that change around anymore, we can run git pop. Typically I default to git pop since I’ll probably commit those changes the second time around.

# apply changes and keep them on our stash list
git stash apply
# apply changes and remove them from our stash list
git stash pop

By default, git stash does not stash new files, however you can stash new files by adding the -u or --include-untracked flag to tell it to stash new files as well. That way everything will be stashed, which can be nice if your changes include additional files:

# stash untracked and edited files
git stash -u

Another nice feature of stash, is to have git iterate over every file that has changed by using the -p or --patch.

➜  dev-diaries git:(master) ✗ git status    
On branch master
Your branch is up to date with 'origin/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:   Makefile
	modified:   package.json

➜  dev-diaries git:(master) ✗ git stash -p  
diff --git a/Makefile b/Makefile
index 6a92c27..0b61fb1 100644
--- a/Makefile
+++ b/Makefile
@@ -13,3 +13,4 @@ dev:
 	$(TTAB) make fe && make app
 
 
+
Stash this hunk [y,n,q,a,d,e,?]? 

diff --git a/package.json b/package.json
index 8457d04..371b45b 100644
--- a/package.json
+++ b/package.json
@@ -26,3 +26,4 @@
     "instafetch.js": "https://github.com/dev-diaries/instafetch.js.git"
   }
 }
+
Stash this hunk [y,n,q,a,d,e,?]? 

Read more about it here

Instagram Post