Table of contents
1.
Introduction
2.
What is Git Stash?
3.
Why is git stash important?
4.
How to use git stash?
5.
Basic Git Stash Commands
5.1.
Managing Git Stashes
6.
Advanced Git Stash Commands
7.
Git Stash V/S Git Commit
8.
Exceptions faced by the user while running the git stash command
9.
Best Practices for Using Git Stash
10.
Frequently Asked Questions
10.1.
What is the purpose of git stash?
10.2.
What is git commit?
10.3.
How to apply a git stash?
10.4.
How do I stash files in git?
10.5.
Differentiate between git push and git commit.
11.
Conclusion
Last Updated: Mar 27, 2024
Easy

Stash Command in Git

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello, Ninja, Looking for ways to shift to a new branch in Git without dropping your committed local changes? Stuck somewhere with git stash commands? You have Coding Ninjas to your rescue. This article will guide you through the different git stash command in Git with their uses.

git stash command

What is Git Stash?

Git stash is a popular git command with the help of which you can temporarily store changes in your directory that are not ready to be committed or are yet to be finished. Stashing helps developers switch to a different branch or work on a different feature, allowing them to work on something else without committing the changes and not losing them simultaneously.

how git stash works?

When we use the git stash command, it takes a screenshot of the working directory. The stash comprises all the changes in the code, new files added, and files deleted. Git stash can also be used to undo the changes temporarily if you do not want them to commit.

Also see,  Install Homebrew

Why is git stash important?

Git stash is important because it allows you to save changes in your work temporarily, like putting your work aside on a desk before focusing on something else. This is handy when you're in the middle of coding and need to switch to a different task or branch. 

Stashing prevents you from losing your progress and keeps your working directory clean. It's like a safety net for your changes, letting you store them safely and reapply them later when you're ready, helping you maintain a tidy and organized version history in your Git repository without the risk of losing work.

How to use git stash?

Git stash is like a secret drawer in your workspace. When you need to switch tasks or store your changes temporarily without committing them, here's how to use it:

  • Stash Changes: If you have unfinished work, stash it with git stash. Your changes are saved, and your workspace is clean.
     
  • Switch Branches: You can switch branches without worrying about unfinished work.
     
  • Reapply Changes: When you return to your original task, use git stash apply to bring back your saved changes.
     
  • Clear Stash: If you're done with the stash, clear it with git stash drop.
     
  • Multiple Stashes: You can stash multiple changes and choose which one to apply when needed.

Basic Git Stash Commands

Let us go through some of the basic git stash commands.

1. Creating a stash with git stash save

You can use the git stash save command to stash your unfinished or uncommitted changes. This saves the modifications in the code, which can be reapplied later.

Syntax:

git stash save


Output:

git stash save

2. Naming stashes with git stash save <name>

You can also stash your changes with a message similar to a commit message which helps you recognize a specific stash.

Syntax:

git stash save <message>


Output:

git stash save

Managing Git Stashes

The following commands can be used to manage git stashes.

3. Listing stashes with git stash list

Whenever you stash the changes in git, they are added to a stack. To display the list of stashes created previously that are stored in the stack, you can use the git stash list command. 

Syntax:

git stash list


Output:

git stash list

The list displayed also contains the date and time when the stash was created, along with the stash message.

The command helps keep a record of the stashes created and selects the specific stash which is to be applied or deleted. 

4. Applying a stash with git stash apply.

We can use the git stash apply command to apply the changes stashed earlier. The command is used when the developer has temporarily saved the uncommitted changes in the working directory but now wishes to reapply the changes.

Syntax:

git stash apply


Output:

git stash apply

To apply the particular nth stash

git stash apply stash@{n}

 

It should be ensured that the changes applied with the git stash apply command are not removed from the stash.

5. Retrieving a stash with git stash pop

The git stash pop command not only retrieves the stashed changes but also removes the stash from the stack.
Syntax:

git stash pop


Output:

git stash pop

6. Applying a stash to a different branch with the git stash branch

To save the changes on a different branch, you can use the git stash branch command. This command helps in avoiding merge conflicts.

Syntax:

git stash branch <branch_name>


Output:

git stash branch

7. Removing stashes with git stash drop

This command is used to delete the most recent stash or a particular stash which is no longer needed.

Syntax:

git stash drop stash@{12}


Output:

git stash drop

8. Clearing all stashes with git stash clear

The git stash clear command is used to remove all stashes from the stack where the stashes created previously are stored.

Syntax:

git stash clear


Output:

git stash clear

Advanced Git Stash Commands

Now that you are well versed with the basic git stash commands, let use dive into some advanced git stash commands.

9. Stashing untracked files with git stash save --include-untracked

You can also stash untracked files using the git stash command. An untracked file in git is created in the repo's working directory but is not yet added using the "git add" command.

Syntax:

git stash save –include-untracked

 

Or you can also use 

git stash -u


Output:

git stash save --include-untracked

10. Stashing ignored files with git stash save --all

The git stash command saves only the tracked changes by default. You can use the git stash save –all command to save the changes of untracked and ignored files. Here –all imply that all changes, including the, tracked and ignored files, will be stashed.

Syntax:

git stash –all <stash_message>

 

Output:

git stash save --all

11. Viewing the contents of a stash with git stash show

With the help of the above command, you can view the changes saved in the stash. It is helpful to verify the changes before applying them.

Syntax:

git stash show // this displays the changes in the topmost stash
git stash show stash@{n} // this displays the changes in nth stash
git stash show -u // this displays the changes of both tracked and untracked files in the topmost stash
git stash show -p // this displays the changes in the topmost stash in a detailed manner, also known as patch format


Output:

git stash show

12. Applying only certain changes from a stash with git stash apply --index

When a stash is applied, the changes are applied to both tracked and untracked files by git. Nevertheless, if the changes to the tracked files present any conflict, the stash may not be applied smoothly. Here comes the use of git stash apply –index. With this command, any modifications staged before the stash creation are reapplied when the stash is applied.

Syntax:

git stash apply –index


Output:

git stash apply --index

Git Stash V/S Git Commit

Git commit and git stash are widely used git commands in software development projects. Hence it becomes more important not to be confused between the two. The differences between git stash and git commit commands are shown in the table below.

git stash

git commit

The git stash command saves the changes temporarily, which can be reapplied later. The git commit command saves changes permanently to the history of the repository
Git stash does not affect the history of the repository. The stashes are only visible to the developer who has created them. After using the git commit command, the changes become a part of the repository's history. The committed changes are visible to anyone who has access to the repo.
Stashing does not require the changes to be staged. For committing changes, the developer must use the "git add" command to stage them.

Exceptions faced by the user while running the git stash command

While running git stash commands, the user might come across certain exceptions also. They are:

  • When the user has not performed any changes in the code and tries to use the git stash command, an exception of 'No local changes to save' is thrown. Thus it would be best to make some changes before stashing them.
     
  • Whenever a user tries to stash changes in the middle of a merge, an exception of 'cannot stash in the middle of merge is thrown'. Thus it is recommended first to resolve the merge conflicts.
     
  • An exception of 'the stash entry is invalid' is thrown when the user tries to apply or pop a stash that does not exist in the stash stack. Thus it is recommended to always check the stash list before applying.
     
  • An exception of 'your local changes to the following files would be overwritten by merge' pops up when the stash applied conflicts with changes in the working directory. The conflicts should be resolved before applying the stash.
     
  • An error of 'your local changes would be overwritten by checkout 'is encountered when the user tries to switch to a different branch while there are still changes in the current working directory to be staged. To resolve this, the changes must either be committed or stashed.

Best Practices for Using Git Stash

By the effective use of stash, you can improve your workflow and productivity. Some of the best practices of using git stash are 

  • It is recommended to use meaningful stash names for your stash. This makes it easier to recognize the stashes later. For example, you can use the command "git stash save 'added new page'" to give your stash a descriptive name.
     
  • Stash frequently to avoid losing your code.
     
  • Conflicts should be avoided when applying stashes. You should review your stashes using the command "git stash list" before applying them. 
     
  • Delete old stashes regularly. Clearing up the stash when it is no longer needed is a good practice.

Frequently Asked Questions

What is the purpose of git stash?

Git stash saves your changes temporarily without committing them to the working directory. It allows users to work on a separate feature without losing the previous changes.

What is git commit?

The git commit command is helpful in saving the changes made in the code to the local repository after they are staged with the help of the git add command. You can also use a commit message with the commit command to identify your commits.

How to apply a git stash?

The "git stash apply" command is used to apply the previously stashed changes. It should be noted that the changes being applied with the git stash apply command are not deleted from the stash.

How do I stash files in git?

To stash files in Git, use 'git stash' command. It helps you save your work without committing changes. Later, you can apply or pop the stash.

Differentiate between git push and git commit.

The git commit command can be used to save the changes made in the source code to the local working directory, whereas git push updates the committed changes to the remote directory.

Conclusion

Hats off, Ninja, You deserve a round of applause for finishing this informative read! We have discussed stash command in Git deeply along with their purpose and syntax.

Keep learning! We recommend you check out some of our other Git-related articles. 

  1. Working with git
  2. Git activity commands
  3. Git interview questions
  4. Ping command in linux
     

Refer to our Guided Path to enhance your knowledge in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. You must look at the problemsinterview experiences, and interview bundles for placement and interview preparations.

Let your curiosity lead the way!

Live masterclass