Home Icon Home Resource Centre Git Stash | How-To, Commands, Options, Uses & More (With Examples)

Git Stash | How-To, Commands, Options, Uses & More (With Examples)

The git stash command is used to temporarily save changes you've made in your working directory without committing them. This allows you to switch branches or work on something else without losing your current progress.
Shivangi Vatsal
Schedule Icon 0 min read
Git Stash | How-To, Commands, Options, Uses & More (With Examples)
Schedule Icon 0 min read

Table of content: 

  • Introduction To Git stash
  • Importance Of Git Stash
  • How To Use Git Stash Command?
  • Git Stash Commands
  • Git Stash Options
  • Git Stash vs. Git Commit
  • Git Stash vs. Git Reset
  • Git Stash vs. Git Stage
  • Best Uses/ Practices Of Git Stash
  • Conclusion
  • Frequently Asked Questions
expand icon

Git stash is a handy tool that allows software developers to temporarily store changes in their working directory without committing them to the repository. This powerful feature allows us to switch between branches or work on other tasks without losing progress.

In this article, we'll explore how the Git stash command works when to use it, and some advanced commands and options to help you manage your workflow more effectively. We'll also cover common scenarios and best practices to ensure you can seamlessly integrate Git stash into your version control toolkit.

Introduction To Git stash

Workflow of Git stash

Git stash is a powerful Git command that temporarily shelves (or saves) changes in your working directory without committing them. This feature is particularly useful when you need to switch contexts or work on something else without losing your current progress. By stashing your changes, you can come back to them later without worrying about uncommitted files or conflicts.

Syntax Of Git Stash

The basic syntax of the Git stash command is as follows:

git stash

This command saves both staged and unstaged changes and reverts the working directory to the state of the last commit.

Importance Of Git Stash

The git stash command plays a vital role in maintaining a smooth workflow, especially when working on multiple features or fixes simultaneously. It can be helpful in several situations, such as:

  • Switching to Another Branch: When you're working on a feature git branch and need to switch to another branch (e.g., for an urgent bug fix), Git stash allows you to save your changes without committing them. This prevents incomplete work from being committed and keeps your commit history clean.
    After stashing your changes, you can switch branches using git checkout <branch-name> and later reapply your stashed changes when you return to your original branch.

  • Resolving Merge Conflicts: During a merge, conflicts might arise that need to be resolved. The git stash command can be used to temporarily save your current changes, allowing you to address the merge conflicts on a clean working directory. Once the conflicts are resolved, you can reapply your stashed changes and continue your work seamlessly.

  • Experimenting with Code: Git stash allows you to save your current work temporarily when you want to try out a new idea or experiment with changes but aren’t ready to commit them. This way, you can test your new approach without worrying about affecting your main codebase. If the experiment works out, you can apply the stash and continue, or simply drop the stash if you decide to discard the changes.

  • Prevents Accidental Data Loss: You might accidentally lose unsaved changes by resetting your branch or checking out another one. By stashing your changes, you safeguard them from accidental loss. Git stash acts as a safety net, ensuring that your work is preserved even when performing potentially destructive operations.

How To Use Git Stash Command?

Using Git stash is a straightforward process. Here are the basic steps to follow:

  1. Make Changes to Your Working Directory: First, make changes to your working directory as you normally would. This includes adding, modifying, or deleting files.

  2. Check Your Git Status: Before stashing your changes, it’s a good idea to check your Git status using the git status command. This will show you the changes you have made to your working directory, including modified, added, or deleted files.

  3. Stash Your Changes: To stash your changes, use the git stash command. This will save all the changes you have made to your working directory (including staged changes) in a new stash entry. By default, Git assigns a name to the stash entry, such as stash@{0}, stash@{1}, and so on.

  4. Check Your Stash List: Use the git stash list command to see a list of all your stashes. This will show all the stash entries you have created so far, along with their respective IDs and messages (if any).

  5. Apply a Stash Entry: To apply a stash entry to your working directory, use the git stash apply option, followed by the ID of the stash entry you want to apply (e.g., git stash apply stash@{1}). This will apply the changes in the stash entry to your working directory without removing it from the stash list.

  6. Pop a Stash Entry: Alternatively, you can use the git stash pop command to both apply and remove the most recent stash entry from your stash list. If you want to pop a specific stash entry, use its ID (e.g., git stash pop stash@{1}).
  7. Create a Branch from a Stash Entry: To create a new branch from a stash entry, use the git stash branch branch_name stash@{n} command, where branch_name is the name of the new branch and stash@{n} is the ID of the stash entry you want to apply. This will create a new branch with the changes from the stash entry, allowing you to continue working on them separately.

  8. Drop a Stash Entry: To remove a stash entry from your stash list, use the git stash drop stash@{n} command, where stash@{n} is the ID of the stash entry you want to remove. This will permanently delete the stash entry, and this action cannot be undone.

Git Stash Commands

List of Git stash commands

When working with Git stash, understanding the available commands is essential for effectively managing your changes. Below, we’ll explore some of the most commonly used Git stash commands and how they can help streamline your development process.

Git Stash List Command

This git stash command is used to list all the stash entries currently saved in your repository. Each stash entry is identified by a unique name like stash@{0}, along with a brief message describing the changes stashed. You can optionally provide Git log options to filter or customize the list output.

Syntax:

git stash list

Example:

$ git stash list
stash@{0}: WIP on main: 1a2b3c4 update index.html
stash@{1}: WIP on main: 4d5e6f7 add new styles

Git Stash Show Command

This git stash command is used to show the changes recorded in a stash entry. By default, it shows a summary of the changes, but you can use the -p option to see the full difference. You can also include or exclude untracked files in the display.

Syntax:

git stash show stash@{1}
git stash show -p stash@{0}
git stash show --include-untracked stash@{0}

Example:

$ git stash show stash@{0}
index.html | 2 +-
style.css | 1 +

Git Stash Drop Command

This git stash command is used to delete a specific stash entry identified by its index (e.g., stash@{1}). If no stash is specified, it deletes the latest stash.

Syntax:

git stash drop stash@{1}
git stash drop

Example:

$ git stash drop stash@{0}
Dropped stash@{0} (WIP on main: 1a2b3c4 update index.html)

Git Stash Pop Command

This git stash command applies the changes from a stash to your working directory and removes the stash entry from the list. If no stash is specified, it pops the most recent one.

Syntax:

git stash pop
git stash pop stash@{2}
git stash pop --index

Example:

$ git stash pop stash@{1}
On branch main
Changes applied, and stash@{1} dropped

Git Stash Apply Command

This git stash command is used to apply the changes from a specific stash entry to your working directory without removing it from the stash list.

Syntax:

git stash apply
git stash apply stash@{0}
git stash apply --index

Example:

$ git stash apply stash@{0}
On branch main
Changes applied from stash@{0}

Git Stash Branch Command

This git stash command is used to create a new branch and apply a specific stash to it. If no stash is specified, the most recent stash is used.

Syntax:

git stash branch new-feature
git stash branch bugfix stash@{1}

Example:

$ git stash branch new-feature stash@{0}
Switched to a new branch 'new-feature'
Changes applied from stash@{0}

Git Stash Push Command

This git stash command is used to save changes in your working directory to a stash. It includes various options to control what gets stashed, such as -u to include untracked files or -a to include all files, including ignored ones.

Syntax:

git stash push -m "WIP: new feature"
git stash push -u
git stash push --patch
git stash push --keep-index

Example:

$ git stash push -m "WIP: refactor login"
Saved working directory and index state WIP on main: 1a2b3c4 WIP: refactor login

Git Stash Save Command

This git stash command is deprecated in favor of git stash push. It saves changes to the stash with options like -u to include untracked files or -p to interactively select changes to stash.

Syntax:

git stash save "WIP: update header"
git stash save -u "Include untracked files"

Example:

$ git stash save "WIP: new footer"
Saved working directory and index state WIP on main: 1a2b3c4 WIP: new footer

Git Stash Clear Command

This git stash command is used to permanently delete all stash entries from the stash list. Since this command cannot be undone, so we need to use it with caution.

Syntax:

git stash clear

Example:

$ git stash clear
Cleared all stashes

Git Stash Create Command

This git stash command is used to create a stash entry from the current working directory and index but does not save it in the stash list. It is generally used for advanced scripting and temporary stashing.

Syntax:

git stash create "Temporary stash for testing"

Example:

$ git stash create "Temporary stash"
1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0

Git Stash Store Command

This git stash command is used to store the provided commit as a stash entry and update the stash reflog. It is used in conjunction with git stash create to save a stash entry that wasn’t automatically added to the list.

Syntax:

git stash store -m "Saved stash" <commit>

Example:

$ git stash store -m "

Git Stash Options

Below is a list of some useful options you can use with Git stash:

  • -u: This git stash option stashes not only the changes to tracked files but also untracked files (files that Git is not yet tracking). It’s useful if you have new files that you want to include in your stash. For example-

git stash -u

  • -a: This git stash option extends the functionality of -u by also stashing ignored files (files listed in .gitignore). It’s a more comprehensive way to stash everything in your working directory. For example-

git stash -a

  • -p: This git stash option provides a detailed difference of the most recent stash, allowing you to review all the changes made in the stash. This is useful for understanding exactly what was saved. For example-

git stash show -p

  • -m: This git stash option adds a custom message to the stash entry. This helps identify the purpose of the stash later when reviewing the stash list. For example-

git stash push -m "WIP: new feature development"

  • -q: This git stash option suppresses the usual output messages from Git, making the commands run quietly. For example-

git stash push -q

  • -S: This git stash option (capital S) stashes only the changes that are currently staged. This is similar to making a commit but saves the changes in the stash instead of committing them to the branch. For example-

git stash push -S

  • --index: This git stash option tries to restore not only the changes in your working directory but also the ones staged (in the index). However, if there are conflicts, it might fail. For example-

git stash apply --index

  • --keep-index: This git stash option stashes only the changes that are staged for commit, leaving the working directory changes intact. For example-

git stash --keep-index

  • --all: This git stash option includes all changes, including ignored files (i.e., files specified in .gitignore), in the stash. For example-

git stash --all

Git Stash vs. Git Commit

Git commit records your staged changes in the repository's history, creating a snapshot of your project at a specific point in time. The key differences between git stash and git commit are as follows:

Feature Git Stash Git Commit
Purpose Temporarily saves changes without committing them to the branch. Saves changes permanently as a snapshot in the project history.
Visibility Changes are saved locally and are not visible in the branch history. Changes are visible in the branch history and shared with others.
Impact on Repository History No changes to the commit history; it's temporary storage. Creates a new commit, altering the history of the current branch.
Usage Used when you need to switch tasks without committing your work. Used when you want to record changes permanently in the Git repository.
Reusability Changes can be reapplied or discarded later. Changes are permanent and part of the project's history.
Command git stash git commit

Git Stash vs. Git Reset

Git reset is used to undo changes by moving the current branch pointer to a different commit. The key differences between git stash and git reset are as follows:

Feature Git Stash Git Reset
Purpose Temporarily saves changes without losing them. Reverts the working directory and index to a previous commit state.
Data Preservation Keeps changes safe for future use. Can potentially discard changes permanently, depending on the reset mode.
Use Case When you need to save work-in-progress without committing. When you need to undo changes or revert to a previous commit.
Impact on Repository History No change in the commit history. Can alter commit history by moving the HEAD pointer.
Options Can apply, drop, or pop stashes as needed. Soft, mixed, and hard resets offer varying degrees of change reversion.
Command git stash git reset [options]

Git Stash vs. Git Stage

Git stage refers to the process of marking files to be included in the next commit. The key differences between git stash and git stage are as follows:

Feature Git Stash Git Stage (Git Add)
Purpose Temporarily saves changes without committing them. Prepares changes to be committed by adding them to the staging area.
Scope Saves all uncommitted changes in the working directory. Adds specific files or changes to the staging area.
Visibility Changes are hidden and not visible in the working directory. Changes are staged and visible in the staging area.
Use Case When you need to save progress temporarily without affecting the working directory. When you are ready to commit certain changes.
Impact on Workflow Helps switch tasks without losing progress. Prepares changes for the next commit.
Command git stash git add

Best Uses/ Practices Of Git Stash

Git stash is a versatile tool that can greatly enhance your development workflow when used effectively. Here are some best practices to consider:

  1. Quickly Switch Contexts: Use the git stash command to temporarily set aside changes when you need to switch to another branch or task without committing to unfinished work. This keeps your working directory clean and avoids unnecessary commits.

  2. Save Partial Work: When you’re midway through implementing a feature but need to focus on something else, stashing your changes ensures you can return to your half-done work without losing progress. Add a descriptive message to your partial stash for easier recall.

  3. Experiment Safely: If you want to test out a risky change or explore a new approach, stash your current work before experimenting. This way, you can easily revert back to your original state if things don’t work out.

  4. Resolve Merge Conflicts: When dealing with merge conflicts, stash your changes first to allow a clean merge process. Once the conflicts are resolved, you can apply the stash and continue working.

  5. Clean Up Untracked Files: Use git stash -u to stash both tracked and untracked files before switching branches. This prevents any untracked stash files from interfering with your work on other branches.

  6. Create Branches from Stash: If you realize that your stashed changes deserve their own branch, use git stash branch branch_name to create a new branch with the stashed work applied. This allows you to continue development with proper version control.

  7. Regularly Clear Unused Stashes: Keep your stash list manageable by regularly clearing out old or unused stashes with git stash drop or git stash clear. This prevents clutter and ensures you only keep relevant changes.

Conclusion

In the fast-paced world of software development, Git stash is an invaluable tool in Git version control kit that enhances productivity by allowing us to save our work-in-progress changes without committing them. Whether we're switching branches, pulling updates, or resolving conflicts, stashing ensures that our work remains safe and easily retrievable.

By mastering Git stash, we can maintain a clean and organized workflow, minimizing the risk of losing important changes and maximizing our efficiency in managing code. As we continue to explore and integrate more advanced Git features, understanding the importance of Git stash is a key step in becoming a more proficient and agile developer.

Frequently Asked Questions

Q. How do I view the list of all stashes I have saved?

To view a list of all saved stashes, we use the git stash list command. This will display a list of stashes along with their references and optional messages.

git stash list

Q. How can I apply a specific stash to my working directory?

To apply a specific stash, use the git stash apply command followed by the stash reference. For example, git stash apply stash@{1} will apply the second most recent stash.

git stash apply stash@{n}

Q. What is Git stash, and why should I use it?

Git stash is a tool that allows us to temporarily save changes in our working directory that are not ready to be committed. This is useful when we need to switch to another branch or work on something else without losing our current changes. It helps us keep our working directory clean and organized by allowing us to apply changes later.

Q. What’s the difference between git stash pop and git stash apply?

The git stash apply command applies the stashed changes to your working directory but leaves the stash entry in the stash list. On the other hand, git stash pop applies the stashed changes and then removes the stash entry from the stash list.

git stash apply
git stash pop

Q. How can I remove a stash without applying it?

To remove a specific stash without applying it, use the git stash drop command followed by the stash reference. This permanently deletes the stash from the stash list.

git stash drop stash@{n}

Suggested reads:

  1. Git Vs. GitHub | 11 Differences, Applications, Prices & More
  2. What Is GitHub? An Introduction, How-To Use It, Components & More!
  3. Git Hooks | Definition, Usage, Types, Workflow & More (+Examples)
  4. Git Cherry Pick Command | How To Use, Undo, Resolve Conflicts & More!
  5. Git Submodule: Add, Remove, Pull Changes & More (With Examples)
Edited by
Shivangi Vatsal
Sr. Associate Content Strategist @Unstop

I am a storyteller by nature. At Unstop, I tell stories ripe with promise and inspiration, and in life, I voice out the stories of our four-legged furry friends. Providing a prospect of a good life filled with equal opportunities to students and our pawsome buddies helps me sleep better at night. And for those rainy evenings, I turn to my colors.

Tags:
Computer Science Engineering Placements

Comments

Add comment
No comments Image No comments added Add comment
Powered By Unstop Logo
Best Viewed in Chrome, Opera, Mozilla, EDGE & Safari. Copyright © 2024 FLIVE Consulting Pvt Ltd - All rights reserved.