Git Stash: Tutorial & Tips (2023)

A tutorial explaining the process of creating, applying, and managing stashes using the 'git stash' command.

TL;DR

Use git stash to save changes without committing them before switching to a different branch

Written by @bazamel_

Image cover

The command “git stash” temporarily saves the current changes in a separate location, allowing you to switch to a different branch without committing the changes.

How do I use it?

Using the git stash command

Sometimes, when working on a Git project, you may find yourself in a situation where you need to switch to a different branch or task, but you’re not ready to commit your current changes. In such cases, the git stash command can prove to be very useful.

How git stash works

The git stash command allows you to save your changes in a temporary storage area called the stash. It’s like creating a checkpoint of your work that you can come back to later.

When you run git stash, Git will take all your uncommitted changes, including those staged for commit, and revert your working directory to the last commit. It also saves the changes in the stash.

Once your working directory is clean, you can switch to another branch or task without any conflicts. When you’re ready to go back to your previous work, you can retrieve the changes from the stash.

Example usage

Here’s a step-by-step example of how to use the git stash command:

  1. Make changes to your code or files in the working directory.
  2. Stage the files you want to commit using git add.
  3. Run git stash:
$ git stash
  1. Git will save your changes in the stash and revert your working directory to the last commit.
  2. Switch to a different branch or work on a different task.
  3. When you’re ready to continue your previous work, switch back to your original branch or task.
  4. Retrieve the changes from the stash using git stash apply:
$ git stash apply
  1. Git will apply the stashed changes to your working directory, allowing you to continue where you left off.

Remember that git stash is not a replacement for commits. It’s a temporary solution to save your changes while working on different branches or tasks without interfering with your progress.

Why is it useful?

Remember, git stash is a powerful tool to temporarily save your changes, switch branches or fix conflicts, and then reapply the saved changes when needed - making it an invaluable command in your Git workflow.

Interesting options

Git Stash

Git stash allows you to save changes in a temporary area so that you can switch branches or work on something else without committing those changes.

Options:

The git stash command offers various options to stash and manage changes in your Git repository.

Best practices

Best Practices for Using git stash

When using git stash in your development workflow, keep the following best practices in mind:

Remember, effective usage of git stash can streamline your development process, allowing you to work on separate tasks without the need for multiple branches.