Jakub Jarosz

Security, Systems & Network Automation

Using Git Stash: A Simple Step-by-Step Guide

2024-12-09 Tutorial

The sprint started yesterdy. We agreed about what product features we work over the next two weeks. started working

Creating a stash

git stash

By default this will create an entry stored in refs/stash with the title “WIP on <your_branch_name***>”***, but you can also customize this message to make it easier to identify individual stash entries:

git stash -m "Your custom message here"

Listing stashes

git stash list

Each stash is given an index, starting from stash@{0} for the most recent entry, in addition to the message referenced in the previous section.

Applying stashes

git stash apply

For applying changes from a specific stash, use its index:

git stash apply stash@{n}

git stash pop vs. git stash apply

Pop: git stash pop applies the most recent stash and then removes it from the stash list. Apply: git stash apply applies the stash but keeps it in the stash list.

Create a branch from a stash

If the changes in a stash are extensive or complex, you might want to create a new branch for them:

git stash branch your_new_branch_name stash@{n}

This command creates a new branch, checks it out, and applies the specified stash. The stash is then dropped if applied successfully.

Stashing untracked files

By default, git stash does not include untracked files. To include them run:

git stash push -u
git stash push --include-untracked

Cleaning up stashes

To remove a specific stash:

git stash drop stash@{n}

To clear all stashes in the stack run:

git stash clear

Stashing specific files

git stash does not support stashing specific files directly. However, you can achieve this by selectively adding files to the staging area and then using git stash --keep-index to stash everything else:

  • Add specific files to the staging area with git add.
  • Run git stash –keep-index to stash changes not in the staging area.
  • Optionally, you can stash the staged changes as well by running git stash again.

Troubleshooting git stash

Aborting an in-progress stash pop/apply

When running git stash pop or git stash apply Git will attempt to apply the stashed changes directly on top of the current commit. This may result in merge conflicts.

In the case you want to abort a stash that’s in progress instead of resolving the conflicts manually, run:

git reset --merge

This will discard all changes in the working directory but will not erase the stash.

  • Stash apply error: If you encounter errors when applying a stash, it might be due to conflicts. Resolve the conflicts manually and then commit the changes, or abort the stash using the step above.
  • Not a valid reference: Ensure you are referencing the stash correctly, e.g., stash@{n}.

Use cases

git stash is useful in various scenarios, such as:

  • Switching branches to work on a different task without committing incomplete work.
  • Keeping a clean working directory when pulling updates from the remote repository.
  • Experimenting with changes that you may want to discard later.

For additional reading please see the official git documentation on git stash.