Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Git Tutorial
  • Git Exercises
  • Git Basic Commands
  • Git Cheat Sheet
  • Git Interview Questions
  • Git Bash
  • GitHub
  • Git Branch
  • Git Merge
  • Git WorkFlow
  • Git Hooks
  • Git LFS
  • Git Rebase
  • Git Cherry Pick
Open In App
Next Article:
Git | Working with Stash
Next article icon

Git | Working with Stash

Last Updated : 08 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Pre-requisites: Git

Git allows multiple users to work on the same project simultaneously. Suppose a developer is working on a feature in a branch and he needs to pull changes from some other developer's branch or if he has to work urgently on some other feature, but the feature he is currently working on is incomplete. In this case, you cannot commit the partial code of the currently working feature. To add this new feature, you have to remove your current changes and store them somewhere else. For this type of situation, Git offers a very useful command known as 'git stash'. git stash command saves the previously written code and then returns to the last commit for a fresh start. Now you can add the new feature without disturbing the old one as it is saved locally. After committing to the new feature you can go on with working on the old feature which was incomplete and uncommitted.

Git stash vs Git Commit

There are two alternative ways to save changes to the git repository: stash and commit.

Git stash: The Git stash command can be used to accomplish this if a developer is working on a project and wants to preserve the changes without committing them. This will allow him to switch branches and work on other projects without affecting the existing modifications. You can roll back modifications whenever necessary, and it stores the current state and rolls back developers to a prior state.

Git commit: Developers wish to permanently store their changes to the source code in the repository history. The "git commit" command can be used to accomplish this. Git creates a new commit that documents your modifications and adds them to the repository history.

To sum up. Git commits permanently save changes to the repository history, while Git stash is used to take a backup of the files we are working on but are not yet ready to be committed.

Git Stash vs Git Reset

Both commands can be combined to manage changes to a Git repository and have various use cases.

Git reset and Git stash With these two commands, we can restore all previously deleted and backed-up files to the current working directory. These two primary distinctions are as follows:

Git Stash

Git Reset

Git stash is used to save the files that are made modifications and are not ready to commit.

Git reset can undo the changes completely and changes the branch pointer to the new commit 

Developers can switch branches and work on other tasks without affecting the current changes.

Changes that have already been made are discarded by Git reset.

Git Stash vs Git Stage

Staging is used to get the changes ready for the subsequent commit, while Git stash is used to take a backup of files that have been modified but are not yet ready to be committed. Both commands can be combined to manage changes to a Git repository and have various use cases.

Git StashGit Stage 
Git stash is used to take the backup of filesFiles are added to the git index using the git stage.

Git stash is used if you need to switch to another task or branch while working on a feature or bug fix.

Git stages modifications are to be included in the subsequent commit when you use the git add command.

Which Folder Stores Git Stash History?

When we use git stash the data will be stored in the form of stashes in our local repository. By default, the data will be stored in the git/refs/stash file. In this file, all the references are stored and the actual data is stored in the .git/objects directory, just like other Git objects. 

The below command is used to list all the stashes that have been created.

git stash list

How Do You Stash Changes In Git?

To save the uncommitted changes for later usage, you can use the 'git stash' command. This command saves your local modifications away and reverts the working directory to match the HEAD commit so that it will give you a clean working directory.

Here are some important options that are most widely used:

  • Git stash
  • Git stash save
  • Git stash list
  • Git stash apply
  • Git stash changes
  • Git stash pop
  • Git stash drop
  • Git stash clear
  • Git stash branch

Stashing Your Work (Git Stash)

git stash
git status
 

 By default, running git stash will stash the changes that have been added to your index (staged changes) and changes made to files that are currently tracked by Git (unstaged changes). To stash your untracked files, use git stash -u.

Managing Multiple Stashes(Git Stash List)

You can create multiple stashes and view them using the 'git stash list' command. Each stash entry is listed with its name (e.g. stash@{1}), the name of the branch that was current when the entry was made, and a short description of the commit the entry was based on.

git stash list
git stash list
 

 To provide more context to the stash we create the stash using the following command:

git stash save "message"

What Are Git Stash Apply And POP(Git Stash Apply & POP)

You can reapply the previously stashed changes with the 'git stash pop' or 'git stash apply' commands. The only difference between both the commands is that 'git stash pop' removes the changes from the stash and reapplies the changes in the working copy while 'git stash apply' only reapplies the changes in the working copy without removing the changes from the stash. In simple words, "pop" removes the state from the stash list while "apply" does not remove the state from the stash list. With the aid of the subsequent command, we can reapply the stored changes. The most current stash will be applied to our working directory, and it will also be taken from the list of stashes.

git stash pop
git stash pop
 
git stash apply
git stash apply
 

 By default 'git stash pop' or 'git stash apply' will reapply the most recently created stash: stash@{0} To choose which stash to apply, you can pass the identifier as the last argument (For eg.:- git stash pop stash@{2}).

Git Stash Show

git stash show command is used to display the summary of operations performed on the stash.

git stash show
git stash show
 

Creating A Branch From Your Stash (Git Stash Branch)

If you want to create and check out a new branch starting from the commit at which the stash was originally created and apply the changes saved in the stash, use 'git stash branch branch_name stash_name'. It drops the stash which is given as the argument and if no stash is given, it drops the latest one.

git stash branch newbranch stash@{0}
git stash branch
 

Cleaning Up Your Stash(Git Stash Clear)

To delete any particular stash (For ex:- stash@{1}), use 'git stash drop stash@{1}'. By default, this command will drop stash@{0} if no argument is provided (git stash drop). To delete all stashes at once, use the 'git stash clear' command. 

Git Stash Clear
 

 

The "git stash" command allows us to keep uncommitted modifications so we can use them as needed in the future. When we use the "git stash" command, the files are automatically backed up. This allows us to switch branches, conduct our chores, and then easily get the files back when we're ready to work on them again.

Git stash
 

Stashing Untracked Or Ignored Files

We can use the git stash command to stash the files which are untracked or ignored files. By following the below steps we can stash our files:

Step 1: Stash the changes for that using the below command.

git stash save --include-untracked

--include-untracked by this it will stash all the untracked files also.

Step 2: Verify the stash: 

Use the below command to confirm that the stash was created:

git stash list

Step 3: By using the following command we can bring back back and apply the stash.

git stash apply stash@{0}

Step 4: Once completion of bringing the stash back if it is unwanted we can delete the stash by using the following command. 

git stash drop stash@{0}

Git Stash Best Practices

Here are some best practices for using the "Git stash" command.

  1. Don't overuse git stash: Git stash must be used moderately whenever it is very important to work on another task then only we should use git stash it will affect the changes that have to be made in committing.
  2. Avoid Unnecessary messages: Using git stash, you can add a message to describe the changes you are stashing. Make the message meaningful. When you need to remember what's in the stash later, this will come in handy. Employ statements that are informative and evocative and that accurately reflect the changes being concealed.
  3. Employ branches: Stashing is a helpful tool when working with Git branches. Before stashing changes, make a new branch so you can switch to different tasks or branches without affecting your current changes. Revert back to the branch and apply the stash when you're ready to work on the modifications once more.
  4. Changes should be made as quickly as feasible: storing should only be used as a temporary solution. Once you have finished making a set of changes, you should commit them to the repository to preserve a record of the changes.

Next Article
Git | Working with Stash

N

nooooob
Improve
Article Tags :
  • Technical Scripter
  • Git

Similar Reads

    Working With Git Repositories
    Git is a powerful and widely-used version control system that helps developers manage their codebases efficiently. By using Git repositories, developers can track changes, collaborate with others, and maintain a history of their project’s development. In this article, we will learn about working wit
    7 min read
    Git - Working Tree
    Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific ap
    4 min read
    Git Stash
    When working on a project, there are times when you need to switch branches or temporarily set aside your changes without committing them. Git provides a feature called Git Stash, which allows developers to save uncommitted changes and restore them later. This is especially useful when working in a
    4 min read
    Staging in Git
    In this article we will try to understand each and every aspect which may help us to understand about Staging in Git. To keep a track of modifications or changes in the file we have to bring that changes to our staging area which we bring by using Staging. Below command and this process of adding th
    8 min read
    Best Tools And Extensions For Working With Git.
    Git has become the go-to version control system for developers worldwide, enabling efficient collaboration, version tracking, and code management. Whether you're working on a solo project or part of a larger team, having the right tools and extensions can enhance your Git workflow. In this article,
    10 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences