“git stash” Command Examples

The git stash command is a versatile tool in Git that allows you to temporarily save your local changes in a “stash,” which is a separate area. This is useful when you need to switch to a different branch, work on something else, or pull changes from a remote repository without committing your current changes. Here’s a more detailed explanation of how git stash works:

  • Stashing Local Changes: The primary purpose of git stash is to save your local changes, which include modifications to tracked files and untracked files, in a temporary storage area called a stash.
  • Scenario – Working on an Unfinished Task: Imagine you’re working on a specific task or bug fix, and you need to switch to a different branch to address an urgent issue. Instead of committing your unfinished changes, you can use git stash to save them without committing.
  • Temporary Storage: The stashed changes are saved in a stack-like structure. You can stash multiple sets of changes, creating a stack of stashes.
  • git stash as a Stack: When you stash changes, they’re added to the top of the stash stack. You can stash multiple sets of changes one after the other.
  • Creating a Stash: The basic command to create a stash is git stash. This command stashes both staged and unstaged changes.
  • Stashing Only Unstaged Changes: If you want to stash only unstaged changes and keep staged changes intact, you can use git stash save –keep-index.
  • Stashing with a Message: You can provide a custom message when stashing changes using the -m or –message flag. This helps you provide context about the stashed changes.
  • Viewing Stash List: You can use git stash list to view the list of stashes you’ve created. Each stash has a unique identifier (stash@{n}) where n is the index of the stash.
  • Applying and Popping Stashes: You can apply the stashed changes to your working directory using git stash apply stash@{n}. To remove the stash from the stack after applying, you can use git stash pop stash@n.
  • Discarding Stashes: If you decide you no longer need a stash, you can use git stash drop stash@{n} to remove it from the stash stack.
  • Branch Switching and Pulling: Stashing is especially helpful when you need to switch branches or pull changes from a remote repository without committing your current changes.
  • Untracked Files: git stash also stashes untracked files, allowing you to save changes to files that are not yet tracked by Git.

“git stash” Command Examples

1. Stash current changes, except new (untracked) files:

# git stash push -m optional_stash_message

2. Stash current changes, including new (untracked) files:

# git stash -u

3. Interactively select parts of changed files for stashing:

# git stash -p

4. List all stashes (shows stash name, related branch and message):

# git stash list

5. Show the changes as a patch between the stash (default is stash@{0}) and the commit back when stash entry was first created:

# git stash show -p stash@0

6. Apply a stash (default is the latest, named stash@0):

# git stash apply optional_stash_name_or_commit

7. Drop or apply a stash (default is stash@{0}) and remove it from the stash list if applying doesn’t cause conflicts:

# git stash pop optional_stash_name

8. Drop all stashes:

# git stash clear

Summary

In summary, git stash is a command that allows you to temporarily save your local changes in a separate area, known as a stash. This is useful when you need to switch tasks, switch branches, or pull changes from a remote repository without committing your ongoing work. It’s a versatile tool that helps you manage your changes and work more efficiently in Git.

Related Post