DEV Community

Cover image for A Comprehensive Guide to Using Git Stash
Md Abu Musa
Md Abu Musa

Posted on

A Comprehensive Guide to Using Git Stash

When working on a Git project, there are times when you need to switch tasks quickly or temporarily set aside your current work. This is where Git Stash comes in handy. It allows you to save your local modifications without committing them, giving you the freedom to switch branches or pull updates without losing your changes. Here's an in-depth guide to the most commonly used Git Stash commands.

1. git stash

The basic git stash command stashes all tracked files that have been modified or staged, but it does not stash newly created files or directories. This is useful when you want to save your changes and switch branches, but you don't need to include untracked files.

git stash
Enter fullscreen mode Exit fullscreen mode

2. git stash -u

The -u option allows you to stash both tracked and untracked files (newly created files and directories). This command is useful when you have new files that you also want to stash alongside your modified files.

git stash -u
Enter fullscreen mode Exit fullscreen mode

3. git stash pop

The git stash pop command applies the most recent stash and then removes it from the stash list. It’s helpful when you want to reapply your saved changes but also want to remove them from the stash history.

git stash pop
Enter fullscreen mode Exit fullscreen mode

4. git stash apply

Similar to git stash pop, this command applies the most recent stash, but it does not remove the stash from the list. This is useful when you want to reapply your changes without losing the saved stash for future use.

git stash apply
Enter fullscreen mode Exit fullscreen mode

5. git stash list

To see a list of all the stashed changes, you can use the git stash list command. It will show you the stash history, allowing you to choose a specific stash to apply or drop.

git stash list
Enter fullscreen mode Exit fullscreen mode

6. git stash pop stash@{n}

If you want to apply and remove a specific stash from the stash list, use the stash@{n} identifier. This allows you to target a particular stash and pop it, which means applying the changes and then removing the stash.

git stash pop stash@{n}
Enter fullscreen mode Exit fullscreen mode

7. git stash apply stash@{n}

You can also apply a specific stash without removing it from the stash list. Use the same stash@{n} identifier but with the apply command.

git stash apply stash@{n}
Enter fullscreen mode Exit fullscreen mode

8. git stash pop clear

If you want to clear all the stashes in your project, you can use the git stash pop clear command. This is a straightforward way to clean up all your stashes once you're done with them.

git stash pop clear
Enter fullscreen mode Exit fullscreen mode

9. git stash drop stash@{n}

To remove a specific stash from the stash list without applying its changes, use the git stash drop command. This is useful when you no longer need a particular stash and just want to clean up.

git stash drop stash@{n}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Git Stash is a powerful feature that allows developers to temporarily save work in progress and switch tasks without losing changes. From stashing new files to applying or removing specific stashes, these commands will help you efficiently manage your workflow.

Happy coding!

Top comments (0)