Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/16/2025 By admin

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

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.

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.

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.

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.

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.

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.

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 ” 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 “

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@{}.

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 for a specific 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 .

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

  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
  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@{}.
  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

Uncategorized

Post navigation

Previous Post: testng
Next Post: Creating Object Repository In Selenium WebDriver Using Properties File As An External Source

Related Posts

Make Selenium Easy – Page 19 Uncategorized
Introduction To Karate – A Unified Framework for API test-automation, mocks, performance-testing, and UI automation Uncategorized
CalTable – Make Selenium Easy Uncategorized
API Testing – REST Assured – Page 6 Uncategorized
postman – Page 2 – Make Selenium Easy Uncategorized
Postman Tutorial Part 12 – Understand Environment & Variables in Postman Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark