Git Stash Guide and Related Commands

Stashing Your Work

The git stash command allows you to temporarily shelve (stash) your changes in your working directory, both staged and unstaged, so you can switch context and work on something else. Later, you can reapply these changes to your working copy.

Example:

$ git status
On branch main
Changes to be committed:
    new file:   style.css

Changes not staged for commit:
    modified:   index.html

$ git stash
Saved working directory and index state WIP on main: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepage

$ git status
On branch main
nothing to commit, working tree clean

Key Points:

Re-applying Your Stashed Changes

Reapplying and Removing from Stash:

$ git stash pop

Reapplies the stashed changes to your working copy and removes the stash.

Reapplying Without Removing:

$ git stash apply

Reapplies the changes to your working copy but retains the stash for future use.

Stashing Untracked or Ignored Files

By default, git stash stashes: