git stash Cheat Sheet

The git stash command is used in Git to save changes that have not been committed to a temporary area, allowing you to switch branches or perform other operations without committing incomplete work. Here’s a git stash commands cheat sheet:

Stash Changes

Stash Working Directory Changes:

git stash

Stash with a Message:

git stash save "Your stash message"

List Stashes

List All Stashes:

git stash list

Apply and Pop Stashes

Apply the Latest Stash:

git stash apply

Apply a Specific Stash:

git stash apply stash@{n}

Replace n with the index of the stash.

Pop the Latest Stash:

git stash pop

Pop a Specific Stash:

git stash pop stash@{n}

Replace n with the index of the stash.

Drop and Clear Stashes

Remove the Latest Stash:

git stash drop

Remove a Specific Stash:

git stash drop stash@{n}

Replace n with the index of the stash.

Clear All Stashes:

git stash clear

Apply Stash to a New Branch

Create and Switch to a New Branch with Stash:

git stash branch <branch_name>

This command creates a new branch and applies the latest stash to it.

View Stash Diff

View Changes in the Latest Stash:

git stash show

View Changes in a Specific Stash:

git stash show -p stash@{n}

Replace n with the index of the stash.

Apply Stash Changes Selectively

Apply Specific Files from the Stash:

git checkout stash@{n} -- <file_path>

Replace n with the index of the stash and <file_path> with the path to the specific file.

Keep Untracked Files

Include Untracked Files in Stash:

git stash -u

This includes untracked files in the stash.

Keep Ignored Files

Include Ignored Files in Stash:

git stash -a

This includes both untracked and ignored files in the stash.

Apply Stash with Conflict Resolution

Apply Stash and Resolve Conflicts:

git stash apply --3way

This attempts to apply the stash with conflict resolution.

These commands should cover the basic use cases of git stash. Always review the official Git documentation on stash for more details and options.