Learn the hidden feature in Git - Stash

Learn the hidden feature in Git - Stash

Learn the hidden feature in Git

ยท

3 min read

Hello everyone ๐Ÿ‘‹,

In this article, Let's discuss about the stash feature in Git. We are going to cover the following topics:

  1. What is Stash?
  2. When to use Stash?
  3. How to use Stash?
  4. How to apply Stash?
  5. How to clear Stash?

1. What is Stash?

The general term of stash means storing something in a hidden place. It is similar in the Git terminology as well. It means moving the changes away from working directory.

2. When to use Stash?

When you don't want to do a commit of half-done work just so you can get back to this point later. The answer to this issue is the git stash command.

3. How to use Stash?

Assume, we have a git repository which has 2 files - index.html & feature-1.js

stash-1.png

In the same master branch, a new feature is developed in the feature-2.js file which is not ready yet.

stash-2.png

Suddenly you got a call from your team lead about a blocker issue in feature 1 & you are requested to fix it immediately. Now, you have to make changes on feature-1.js without pushing feature-2.js.

This is where git stash will come to the rescue.

In this situation,

  1. First add the unfinished files to the staging area by git add command,
    git add feature-2.js
    
  2. Run the below command to move the files from working directory to stash.
    // with -m flag, you can add customized stash message.
    git stash push -m "feature 2 in progress"
    
  3. Then run, git stash list to see the stash list. You will see the stash id along with the message. Here, stash@{0} is the stash id.
    stash@{0}: On master: feature 2 in progress
    
  4. The feature-2.js will not be available in your git status and as well as in your working directory. Now, you can work on feature-1.js and push your fix to the remote repository.

stash-4.png

Well Done! ๐Ÿ‘

You have successfully fixed issue in feature-1.js.

But, how to bring back feature-2.js to the work copy? ๐Ÿค”

That brings to the next section on applying stash.

4. How to apply Stash?

Like how the files were moved from working directory to the stash through git stash push command, one has to use git stash apply command to move the changes from stash to working directory.

To do that, follow the below steps,

  1. To see the list of stashes along with stash id, run the git stash list command. You will see the stash id along with the stash message we have given earlier.
    git stash list
    stash@{0}: On master: feature 2 in progress
    
  2. In this case, stash@{0} is the stash id. Use this in below command to apply the changes.
    git stash apply stash@{0}
    
  3. You will see the following output on running the above command
    On branch master
    Changes to be committed:
    (use "git reset HEAD <file>..." to unstage)
     new file:   feature-2.js
    
  4. In the last line of above log, you can see feature-2.js is bought back from stash. Now, you can work on continue working on Feature 2. ๐Ÿ™Œ

Lets try run git stash list once more.

stash@{0}: On master: feature 2 in progress

You will be surprised to see the Stash is still there. We have already have feature-2.js file, But, why is still in Stash? We will see how to clear from stash on next section.

5. How to clear Stash?

There are 2 ways to clear the applied stash.

  1. You can remove the stash by id. In our case, stash@{0} is the stash id. This will remove only the specific stash from the stash list.

    git stash drop stash@{0}
    
  2. You can completely remove all the stash in the list. Warning: This will remove all the stashes from the stash list.

    git stash clear
    

That's it!

Thanks for reading my article. I hope you've learned something new day!

Did you find this article valuable?

Support Yuvaraj by becoming a sponsor. Any amount is appreciated!

ย