git stash

2021-07-24 hit count image

Let's see how to use git stash to store current modification temporarily on Git.

Outline

When we manage the source code with Git, sometimes, during that we develop a new feature, the bug occurs on the service. At this time, we need to create a new branch to fix the bug. However, current development of the feature is not done, so we can’t create a new branch or checkout other branches.

At this time, we can commit the current modification, but the developing is not done, so it is not suitable to commit. In this case, we can use git stash.

In this blog post, we’ll see how to use git stash.

Store

We can use git stash to store temporarily the modified files that are managed on Git. You can store the current modification contents by executing the following command.

git stash

git stash will store the modified files that are only managed on Git. That means new files are not stored. If you want to store the newly created files, you should execute git add new files for Git to be able to manage. After then, you can store them by execute the git stash command.

git add .
git stash

List

When you store the modified files by git stash, the contents are stored in the stack format. After storing, you can see the saved list by using list option.

git stash list

When you execute the command above, you can see the saved list like the below.

[email protected]{0}: WIP on main: 79fb440 Merge branch 'develop' into main

Load

Next, let’s see how to load the saved contents. To load and apply the stored contents, you can use apply option.

git stash apply

When you execute the command above, you can load and apply the modified code, but when you execute git stash list to see the saved list after appling, you can see the applied contents are still there.

git stash list
# [email protected]{0}: WIP on main: 79fb440 Merge branch 'develop' into main

In this case, you can use drop option to remove the applied contents.

git stash drop

If you want to load and apply the modified files and remove the applied them at the same time, you can use pop option.

git stash pop
# git stash apply
# git stash drop

Completed

Done! we’ve seen how to store the modified contents temporarily with git stash. We should be careful to use git stash that stores only committed files or files that are added by git add. Now, please try to use git stash to store temprarily the modified files and checkout other branches!

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

Posts