GIT Stash Tutorials


In Git, stash is a useful command to temporarily store changes that are not yet ready to be committed. The git stash command takes a snapshot of your working directory, including changes you’ve staged but not yet committed, and saves it on a new stash stack. This allows you to temporarily revert your working directory to a clean state and later restore the changes you stashed.

Here are some examples and complete details of how to use the git stash command:

Stash all changes:

git stash

This command stashes all the changes you’ve made to tracked files that are not yet committed, as well as any changes you’ve staged.

Stash changes with a message:

git stash save "my message"

This command stashes all the changes you’ve made to tracked files that are not yet committed, as well as any changes you’ve staged, with a message “my message” to describe the changes.

Stash changes and include untracked files:

git stash --include-untracked

This command stashes all the changes you’ve made to tracked and untracked files that are not yet committed, as well as any changes you’ve staged.

Stash changes and keep the index (staged changes):

git stash save --keep-index

This command stashes all the changes you’ve made to tracked files that are not yet committed, but does not stash any changes you’ve staged. This allows you to keep the changes you’ve staged and continue working on them, while temporarily reverting the changes you’ve made to the tracked files.

List all stashes:

git stash list

This command lists all the stashes you’ve created, including the stash message, commit hash, and branch.

Apply the most recent stash:

git stash apply

This command applies the most recent stash you’ve created, restoring the changes you stashed to your working directory.

Apply a specific stash:

git stash apply stash@{2}

This command applies the stash with the index 2 (the third stash created), restoring the changes you stashed to your working directory.

Apply and delete the most recent stash:

git stash pop

This command applies the most recent stash you’ve created, restoring the changes you stashed to your working directory, and then deletes the stash from the stash stack.

Delete a specific stash:

git stash drop stash@{2}

This command deletes the stash with the index 2 (the third stash created) from the stash stack.

Apply and delete all stashes:

git stash pop --all

This command applies and deletes all the stashes you’ve created, restoring the changes you stashed to your working directory.

Apply a stash to a new branch:

git stash branch new_branch_name

This command creates a new branch called new_branch_name and applies the most recent stash to the new branch, restoring the changes you stashed.

These are some of the commonly used git stash commands and options that you can use to temporarily store and restore changes in Git.