Developer Guides

How to Use Git Stash and Git Stash Pop Effectively

Learn how to handle incomplete work and urgent tasks seamlessly using git stash and git stash pop.

4 min read

Learn how to handle incomplete work and urgent tasks seamlessly using git stash and git stash pop in this practical guide. These commands allow you to temporarily save your uncommitted changes, switch branches, and retrieve your work without creating unnecessary commits.

Prerequisites for Using Git Stash

Before diving into using git stash, ensure you meet the following requirements:

prerequisites

  • Git is installed and configured on your computer.
  • You have an initialized Git repository with some uncommitted changes.
  • Basic knowledge of Git commands like git status, git branch, and git switch.

Stash Changes in Git

You can use git stash to save your uncommitted changes without committing them to your repository. Follow these steps:

steps

  1. Check the status of your working directory:
    bash
    git status
  2. To stash changes (staged and unstaged):
bash
   git stash # Temporarily save changes
   # Output example:
   # Saved working directory and index state WIP on branch_name: <commit_hash> <message>
  1. To include untracked files, use:
bash
   git stash -u
   # Output example:
   # Saved working directory and index state WIP on branch_name: <commit_hash> <message>
  1. Verify the stashes saved:
bash
   git stash list
   # Output example:
   # stash@{0}: WIP on branch_name: <commit_hash> <message>

Retrieve Stashed Changes Using Git Stash Pop

Once you're ready to continue where you left off, use git stash pop to apply the latest stash.

steps

  1. Switch back to the branch where you want to apply the changes:
    bash
    git switch branch_name
  2. Apply and remove the most recent stash:
bash
   git stash pop
   # Output example:
   # Auto-merging filename
   # Dropped stash@{0
  1. If conflicts arise, resolve them as you would during a merge conflict:
    bash
    git status # Identify conflict files
    git mergetool # Use a merge tool to resolve conflicts

Optional Commands for Advanced Usage

Expand your git stash capabilities with these advanced commands:

steps

  1. Apply a stash without removing it:
bash
   git stash apply
   # Changes applied but stash remains
  1. Apply a specific stash using its identifier:
bash
   git stash apply stash@{1}
   # Applies the second stash in the list
  1. Remove a specific stash:
bash
   git stash drop stash@{1}
   # Removes the specified stash
  1. Clear all stashes:
bash
   git stash clear
   # Removes all stashes from history

Common Pitfalls with Git Stash

When working with git stash, beware of these common issues:

Confirming Your Stash Operations

After each operation, you can verify its results to ensure your changes are saved or restored correctly.

steps

  1. Check the list of stashes:
bash
   git stash list
  1. Confirm retrieved changes are back in your working directory:
    bash
    git status

Summary of Git Stash Operations

FAQ

How can I include untracked files in a stash?

Use the git stash -u command to include untracked files in the stash.

What is the difference between git stash pop and git stash apply?

git stash pop applies the changes from the stash and simultaneously removes the stash. In contrast, git stash apply applies the changes but retains the stash for future use.

What happens during a stash conflict?

Stash conflicts occur when changes in the stash conflict with changes in the working directory or branch. Git will notify you of the conflict, and you'll need to resolve it manually, similar to resolving merge conflicts.

Can I view or restore a specific stash?

Yes, use git stash list to view all stashes and retrieve specific ones using git stash pop stash@{n} or git stash apply stash@{n}.


Official reference: Git reference manual.