How to Use Git Stash to Temporarily Save Changes via Terminal: Step-by-Step Guide

Need to switch tasks but don’t want to lose your current changes?

Git stash is your go-to solution for saving work temporarily without committing it.

In this guide, let's walk you through how to use Git stash in the terminal, so you can safely pause your work and pick it up again whenever you’re ready.

How to Use Git Stash to Temporarily Save Changes via Terminal

Steps to follow:

1. Check your changes before stashing.

2. There are two simple ways to save temporary changes:

  • Using Git Stash without a Message: If you’ve made changes and need to switch branches or save your progress without committing, use this option. (Refer from Step 3)
  • Using Git Stash with a Message: If you’re working on a feature or bug fix and need to stash your changes, you can add a descriptive message for easy reference later. (Refer from Step 4)

3. The '-m' flag lets you add a message to your stash, making it easier to identify its purpose later.

4. To save your changes temporarily, use the 'git stash' command.

5. You can see a list of all stashes using 'git stash list'.

Note: This will show you a list of stashes with their corresponding names, like stash@{0}, stash@{1}.

6. To continue working, use 'git stash apply' to reapply your stashed changes.

By default, it restores the latest stash ('stash@{0}'), but you can specify any stash, e.g., 'git stash apply stash@{1}'.

7. After applying a stash, it stays in the stash list. If you don't need it anymore, remove it with 'git stash drop'.

Note:

- To remove a specific stash, use 'git stash drop stash@{1}'.

- To apply and remove a stash at once, use 'git stash pop', or for a specific stash, 'git stash pop stash@{0}'.

That's All!

We have successfully stashed, applied, and dropped your temporary changes.

Conclusion

Using Git stash is an easy way to keep your working directory clean while switching contexts. Now that you know how to stash and reapply your changes, you’ll have the flexibility to manage multiple tasks seamlessly without losing any progress.

Still have questions?

Send an email to archana@leadwalnut.com, OR

Book a FREE consultation with an expert developer here.