Git Stash

In the dynamic world of software development, interruptions and context switches are common. Git's git stash command comes to the rescue, providing a flexible way to save and apply changes on the fly. We'll explore the diverse applications of the git stash command, offering you a toolkit to manage your work seamlessly.

1. Stash Uncommitted Changes:

Stash changes in your working directory without committing them:

bash
git stash save "Your stash message"

Replace "Your stash message" with a descriptive message about the changes you're stashing. This creates a new stash with a unique identifier.

2. Stash Including Untracked Files:

Include untracked files in your stash using the -u or --include-untracked option:

bash
git stash save -u "Stashing with untracked files"

This is useful when you want to stash both modified and untracked files.

3. Stash Only Untracked Files:

If you want to stash only untracked files without modifying existing tracked files:

bash
git stash save --keep-index "Stashing only untracked files"

The --keep-index option ensures that the changes you've already staged are not stashed.

4. Stash and Include Ignored Files:

To stash changes, including ignored files:

bash
git stash save -a "Stashing changes with ignored files"

This command includes all changes, even those listed in your .gitignore file.

5. List Stashes:

View a list of your stashes:

bash
git stash list

This provides a quick overview of your stashes, showing the stash index, branch, and message.

6. Apply the Latest Stash:

Apply the changes from the latest stash to your working directory:

bash
git stash apply

This command leaves the stash in the stash list, allowing you to apply it again if needed.

7. Apply a Specific Stash:

Apply changes from a specific stash using its index:

bash
git stash apply stash@{2}

Replace {2} with the index of the stash you want to apply.

8. Pop the Latest Stash:

Apply the changes from the latest stash and remove it from the stash list:

bash
git stash pop

This is a convenient way to apply and drop the latest stash in one command.

9. Drop a Stash:

Remove a specific stash from the stash list:

bash
git stash drop stash@{1}

Replace {1} with the index of the stash you want to drop.

10. Clear All Stashes:

Remove all stashes from the stash list:

bash
git stash clear

This action is irreversible, so use it with caution.

11. Create a Branch from a Stash:

Create a new branch and apply changes from a specific stash:

bash
git stash branch new-branch stash@{0}

Replace new-branch with the desired branch name and {0} with the index of the stash.

12. Keep Staged Changes:

Stash only unstaged changes, keeping the staged changes in your working directory:

bash
git stash save --keep-index --include-untracked "Stashing unstaged changes"

This is useful when you want to separate staged and unstaged changes.

13. Stash Interactive Mode:

Stash changes interactively, choosing which changes to stash:

bash
git stash save -p "Stashing interactively"

This opens an interactive prompt, allowing you to select individual changes to stash.