Stash changes

Use git stash when you want to change to a different branch but you have uncommitted changes that are not ready to be committed.

Create stash entries

By default, git stash stores tracked changes in your working directory and any staged changes. You can use options to control which changes are included.

  • To stash tracked changes:

    git stash
  • To stash changes with a message:

    git stash push -m "describe your changes here"
  • To stash changes but keep staged changes in your working directory:

    git stash push -k

    The -k (--keep-index) option stashes your changes but also keeps them in the working directory. Use this option when you want to temporarily save changes but keep working on them.

  • To stash changes and include untracked files:

    git stash push -u

    The -u (--include-untracked) option also stashes files that Git is not yet tracking. Without this option, new files that have not been committed remain in your working directory.

  • To stash only staged changes:

    git stash push -S

    The -S (--staged) option stashes only changes that are staged. Use this option when you want to save staged changes while you keep working on unstaged changes.

Apply stash entries

If you make many changes after you stash your work, conflicts might occur when you apply the stash. You must resolve these conflicts before the changes can be applied.

  • To apply the most recent stash entry and keep it in the stash:

    git stash apply
  • To apply a specific stash entry:

    git stash apply stash@{3}
  • To apply the most recent stash entry and remove it from the stash:

    git stash pop

View stash entries

  • To see all stash entries:

    git stash list
  • To see stash entries with more detail:

    git stash list --stat

Delete stash entries

  • To delete the most recent stash entry:

    git stash drop
  • To delete a specific stash entry:

    git stash drop <name>
  • To delete all stash entries:

    git stash clear

Example: Create and apply a stash entry

To try using Git stashing:

  1. Modify a file in a Git repository.

  2. Stash the modification:

    git stash push -m "Saving changes from edit"
  3. View the stash list:

    git stash list
  4. Confirm there are no pending changes:

    git status
  5. Apply the stashed changes and remove the entry from the stash:

    git stash pop
  6. View the stash list to confirm the entry was removed:

    git stash list