Git Tutorial 22 – Git Stash – Dirty To Clean Working Directory – Save Changes Temporarily

Introduction

As a part of GIT Tutorials End To End, we will learn about the Git Stash command in this post.

Prerequisite posts

I will expect that you are aware of the basic concepts and commands of GIT. But if you are a beginner in GIT then I strongly recommend you to refer GIT Basic Commands and Concepts section on my blog first.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

Why Git Stash?

Let’s perform some steps first.

Let’s clone one of my public repo to practice and learn easily. The same repo I have used in my previous posts. In this repo, I have only the default branch “main” and three commits.

Let’s create a new file, add some content to it and add it to the index i.e. staging area.

As of now, we have some uncommitted changes in the local working repo. For some reason you do not require these changes as of now and but later at some point, you may require these changes. Obviously, you can create a branch and push these changes and checkout the branch whenever you need. But there is another way to achieve the same thing using git stash.

Create a stash or stash your work

The git stash command is used to save the local changes away locally so that you reapply whenever you need.

When we run the git stash command above, it saves the working directory and index state which is labelled as WIP i.e. Work In Progress and restores the commit id 2335839a which is the latest commit i.e. HEAD commit.

There are some important points to know here to know:-

  1. Git stash by default willl not stash untracked (file which is not tracked by git) and ignored (file mentioned in .gitignore file) files.
  2. Git stash will only stash uncommited changes.
  3. Git stash will stash staged files and unstaged changes.

Please note Stage file is a file that is tracked by git, added to the index but not committed and the unstaged file is a file tracked by git and has been modified but changes are not staged. Let’s see these concepts in action now.

Git stash for Untracked File or Ignored file

I create a new file FileF but do not add it to the index or staging area using the git add command. If we run the git stash command now then this untracked file FileF will not be stashed by default. The same behaviour will be applied to ignored files as well.

We can override the above behaviour by providing an extra argument with the git stash command.

Use git stash -u command to stash untracked files.

Use git stash -a command to stash both untracked and ignored files.

Git stash for Committed changes

I committed the change and executed git stash command. You can see below that nothing is stashed as the local working tree is cleaned. Committed changes will not be stashed.

Git stash for Uncommitted and Unstaged changes

I will make some changes in FileF and will not stage the changes. When I run the git stash command, it will stash uncommitted/unstaged changes and restore the latest commit of my local 99e098f.

Now I hope you are clear how stashing works in git. Many think that git stash will stash all changes you have done and you will have a working directory as a newly cloned project. This understanding is incorrect.

Please note here that created stashes are local to you i.e. no one else can see them or available or push to a remote repo.

List created stashes

We can create multiple stashes. It does not matter if contains are the same. We can list all created stash using the command git stash list.

We have created two stashes as of now which are listed above. Each stash will have an index. A new stash will have an index as zero and old stashes will shift accordingly by one. It is called LIFO i.e. Last In First Out.

Default Stash description contains “WIP on <branchName>” and the commit id from where the stash was created. This description will confuse you if there are multiple stashes and you will not be able to figure out the changes it contains. We can provide a custom message while creating a stash to identify it. Use the command git stash save <message>

Reapply stashed changes

We can reapply stash changes whenever we want. That is why we use stashing generally in Git. We can reapply the latest stash using the command git stash apply.

Above I have created a stash. You can see that FileK is not available now after stash. Let me reapply stash.

FileK is available now after reapplying stash.

We can also mention stash ID to reapply. By default, it reapplies the latest stash but if we want to reapply a specific stash then we need to pass the corresponding stash id. Use the command as git stash apply stash@{<index>}.

If we list created stash you will see the reapplied stash are still present. It means we can reapply the same stash multiple times.

If you want to reapply and remove stash then we need to use the command git stash pop for the latest stash or git stash pop <stashId> for a specific stash.

Clear created stash

In the above section, we leant how to apply and remove stash using the pop command. But it is not necessary to reapply stash to remove it. We can clear it directly as well.

If you want to clear all stashes we should use the command git stash clear.

If you want to remove a specific stash using stash id then use the command git stash drop <stashId>.

We have some more concepts of stashing which we will see in upcoming posts.

Summary

  1. The git stash saves uncommitted changes i.e. staged and unstaged changes and overlooked untracked and ignored files by default.
  2. To create a stash use command as git stash.
  3. To create a stash with custom message use command as git stash save <message>
  4. To stash untracked and ignored files as well use command git stash -a.
  5. To reapply latest stash use command as git stash apply.
  6. To reapply a particular stash use command as git stash apply stash@{<index>}.
  7. To clear all stashes use command as git stash clear.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.
#ThanksForReading
#HappyLearning

Leave a Reply

Your email address will not be published. Required fields are marked *