How to use Git stashes as a temporary storage

Let’s say you’re coding on your development branch. And you get a notice that there’s a bug on the production branch.
You want to check for the bug, but you don’t want to lose the work you’ve created on the development branch. You also don’t want to co…


This content originally appeared on Zell Liew and was authored by Zell Liew

Let’s say you’re coding on your development branch. And you get a notice that there’s a bug on the production branch.

You want to check for the bug, but you don’t want to lose the work you’ve created on the development branch. You also don’t want to commit what you’ve written because they’re not done yet.

What do you do? You can’t commit and you can’t switch branches. If you switch branches, things that aren’t committed will flow over to the branch you switched to.

What you want to do is save the changes somewhere temporary while you switch over to another branch. **A Git stash is that temporary storage. **

Using a Stash with Git Fork

To use a stash, you need to start with some uncommitted code. For this lesson, we’re going to use the following piece of code as the uncommitted changes:

<!-- Some uncommitted code in index.html -->
<main>
  <p> A new paragraph</p>
</main>

To stash this code, you can click on the stash button.

The stash button

Once you click on the stash button, Fork will ask you to leave a message. This message indicates what the stash is about.

Since stashes are temporary, you can use whatever name you want. We’re going to call it “Temp storage”.

Naming the stash

Once you create a new stash, you’ll find it in the Stashes section on the sidebar.

The stash can be found in the sidebar

Note: You won’t be able to see the changes in this stash, but that’s not a problem because you won’t have to. What you want to do is switch your branch, finish what you need to do and switch back.

In this case, we’re going to check out to the master branch. When you do so, notice you don’t see the uncommitted code we wrote above in both the master and develop branches.

Applying stashed changes

You can go back to the branch you were at, then right-click on your stash and select Apply stash.

Applying a stash

Fork will ask you whether to delete the stash when you do so. I usually delete the stash because I don’t want to keep more than one stash at one time.

Deleting the applied stash

Once you apply the stash, you’ll be able to see the changes you made.

<!-- You'll see your uncommitted code again! -->
<main>
  <p> A new paragraph</p>
</main>

Wrapping up

Stashes are temporary storages where you can store you code. When you’ve stored your code, you can move to other branches to do something else.

When you’re done, you can put your code back from the stash.

With Git Stash, you won’t have to worry about losing any uncommitted changes!


This content originally appeared on Zell Liew and was authored by Zell Liew


Print Share Comment Cite Upload Translate Updates
APA

Zell Liew | Sciencx (2018-10-12T00:00:00+00:00) How to use Git stashes as a temporary storage. Retrieved from https://www.scien.cx/2018/10/12/how-to-use-git-stashes-as-a-temporary-storage/

MLA
" » How to use Git stashes as a temporary storage." Zell Liew | Sciencx - Friday October 12, 2018, https://www.scien.cx/2018/10/12/how-to-use-git-stashes-as-a-temporary-storage/
HARVARD
Zell Liew | Sciencx Friday October 12, 2018 » How to use Git stashes as a temporary storage., viewed ,<https://www.scien.cx/2018/10/12/how-to-use-git-stashes-as-a-temporary-storage/>
VANCOUVER
Zell Liew | Sciencx - » How to use Git stashes as a temporary storage. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2018/10/12/how-to-use-git-stashes-as-a-temporary-storage/
CHICAGO
" » How to use Git stashes as a temporary storage." Zell Liew | Sciencx - Accessed . https://www.scien.cx/2018/10/12/how-to-use-git-stashes-as-a-temporary-storage/
IEEE
" » How to use Git stashes as a temporary storage." Zell Liew | Sciencx [Online]. Available: https://www.scien.cx/2018/10/12/how-to-use-git-stashes-as-a-temporary-storage/. [Accessed: ]
rf:citation
» How to use Git stashes as a temporary storage | Zell Liew | Sciencx | https://www.scien.cx/2018/10/12/how-to-use-git-stashes-as-a-temporary-storage/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.