My Work Notes

07 Feb 2023

Work with git stash

Stash in Git is a temporary storage for uncommitted changes. It can be used to save changes before switching to another branch or to apply changes from another branch. Here are some useful commands:

Save changes

To simply save changes:

git stash

To save changes with a custom message what can be useful for future reference:

git stash push -m "name or message about changes"

Look at stash

If you want to apply a certain stash entry, you need to specify the stash name.

To get the list of stashes:

git stash list

To get the list of files in the stash:

git stash show stash@{n} --name-only

To get a diff in the stash:

git stash show stash@{n} -p

Apply changes

Firs of all, you need to know that stash is a stack. The last stash is on the top of the stack. To apply changes from the last stash (changes will stay in the stash and you can apply them again):

git stash apply

Next command will apply changes from the last stash and keep it in the stash list (n is the number of stash from the list):

git stash apply stash@{n}

Next command will apply changes from the last stash and remove it from the stash list:

git stash pop stash@{n}

To apply changes using the stash name:

git stash apply^{/name}

To create a branch from stash:

git stash branch branch-name stash@{n}

To create patch from stash:

git format-patch stash@{n}
git diff stash@{n} > patch-name.patch

Delete stash

To delete a stash:

git stash drop stash@{n}

To delete all stashes:

git stash clear