Open In App

Git | Working with Stash

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
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 Stash Git Stage 
Git stash is used to take the backup of files Files 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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads