DEV Community

Cover image for From Changes to Safe Keeping: Git Stash
Farhat Sharif
Farhat Sharif

Posted on

From Changes to Safe Keeping: Git Stash

✦ You’re working on a feature but need to switch branches without losing your work.
✦ You want to save your unfinished work temporarily to a separate place without committing it to your feature branch.

That’s where git stash helps! It lets you store your current changes and gives you a clean working directory, so you can switch tasks without losing any progress.

1. Stash changes:

Command: git stash
Description: Saves your local changes to a separate place and reverts your working directory to the last commit, allowing you to switch branches or work on something else.
Command: git stash push -m "stash message"
Store changes with message. Add a descriptive message so you can identify the work stored in the stash easily.

2. List saved stashes:

Command: git stash list
Description: Shows you a list of saved stashes which you can utilize again.

3. View Stashed Changes:

Command: git stash show stash@{n}
Description: View the changes stored in a specific stash. Stash are referenced by numbers. e.g. stash@{1}

4. Apply a specific stash:

To restore stashed changes you have two options:
a).
Command: git stash apply stash@{n} 
Description: Apply the changes and leave a copy in the stash.

b).
Command: git stash pop stash@{n} 
Description: Apply the changes and delete these changes from the stash.

If you don't use stash reference, you are working with the latest stash:
git stash apply
git stash pop


With git stash, you can save, view, and restore changes whenever needed. Perfect for switching tasks!

Top comments (0)