git stash

Stash is used to temporarily shift the changes made to working repository so that something else can be done there.

Stash saves changes for tracked files and indexed files by default.

Note

Indexed files are newly tracked or staged files.

To save the untracked files we can use -u or --inclusive-untracked flag with git stash.

To save all files we can use -a or --all flag which stashes ignored files as well.

git stash (default stash)
git stash -u (include untracked files)
git stash -a (stash all files)

How stash works?

A stash is also encoded as a commit object. .git/ref/stash points to the latest stash made. Depending on the stash made, there can be multiple stash commits.

If only tracked files are stashed, a single commit is made with parent as current HEAD commit.

If untracked files are included, one more commit is created.

Similarly, commit is created for ignored files.

Git stash commits
fig: Git stash commits

References

  1. https://www.atlassian.com/git/tutorials/saving-changes/git-stash