GIT STASH: Save uncommitted changes (WITH EXAMPLES)

Want to switch the git branch for some other quick fix but don’t want to commit changes in the current branch then the “git stash” command is here for your rescue.

What is Git stash?

Git stash command is used to store changes that you don’t want to commit now and take you to the last commit of the branch. And later when you are ready you can reapply the stashed changes.

Now let's see in more detail with examples of how to use “git stash”.

How to use Git stash command?

The simplest git stash workflow is to stash the changes with the git stash command, work on some other functionality and later apply the stashed changes back to get the modified files.

Here is a step-by-step example of how to use git stash.

1. Check uncommitted changes

Run “git status” to check for uncommitted changes which you want to save.

$ git status
On branch develop
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   index.php

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

2. Run "git stash" command

If you find any changes which you don't want to commit and work on something else, then run "git stash". Git stash will store changes and will give you a clean branch.

$ git stash
Saved working directory and index state WIP on develop: 34cd5ff Test commit.

3. Work on something else

Now you are free to switch to other branches or work on something else without any modified changes in the working directory.

4. Get a clean working directory

Once you are done with other work you need to make sure you have a clean working directory by running "git status".

$ git status    
On branch develop
nothing to commit, working tree clean

5. Run "git stash pop"

Now run the "git stash pop" command to get back your stashed changes. This command reapplies the last stashed change to the working directory and deletes it from the stash.

$ git stash pop        
On branch develop
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   index.php

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (d75596436aa3c3e926061d2213e3f2959bc2b16b)

6. Check modified changes

You can run git status again to make sure everything is okay and you have your modified changes back.

$ git status   
On branch develop
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   index.php

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

 

Advanced Git stash commands

Git stash also comes with many useful advanced commands which will help you in specific scenarios.

  • git stash
  • git stash apply
  • git stash list
  • git stash drop
  • git stash clear
  • git stash pop
  • git stash branch
  • git stash file
  • git stash show

 

More resources

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
1 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.