How to Name and Get a Stash by Name in Git?

613
git stash

Are you familiar with Git? In this article, we will learn how we can stash our dirty working directory with name and later get the stashed. Git stash is mainly used in the following case scenario:

  • We need to do a hard reset on your local working tree;
  • We don’t want to lose your current changes (which would be the result of resetting hard); or
  • We don’t want to commit your local changes because that might break the build.

We can normally use git stash with the following command.

//stash the current working directory
git stash

This command will stash the changes from the last commit and make it clean.

As we can see in the image above, after “git stash” command, the working directory is clean and a stash is saved with index stash@{0}.

If there are a lot of stash in the project, how can we detect which stash contains your changes? For this, we can do a git stash with name or message. We can do it using the following command.

//git stash with message
git stash push -m "name or message"

If we check the stash list, we can see our latest stash with index and message on dev branch.

After successful stash, we can view the stash list using the following command

//retrieve stashed code
git stash list

We can apply and remove the stash from the list using the following command.

//apply and remove stash, n is index of stash
git stash pop stash@{n}

We can also apply the stash without removing the stash from the list using the following command.

//apply without removing stash
git stash apply stash@{n}

we can also apply the stash using stash name instead of stash index using the following command.

//apply with stash name
git stash apply stash_name

Read More Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.