How Git Stash can save your life
Tags: git, github, programming, tutorial
Published: 2023-01-15 10:38:11



![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gkaaylql34l8uacj8xfd.png)

Git Stash is a little-known command by devs but can save you time if you use it correctly. Imagine the following situation: you have a task to solve, and you open your branch from main. Everything is perfect, but at some point, things stop working, chaos and despair take over your thinking, and at this point, you want to erase everything you’ve done to make sure that this error is coming from the code you wrote.

Most developers would commit their code even if it didn’t work, checkout the main to see if it worked, and then go back to the branch they were working on. In this case, the correct (and less laborious) way is to use git stash.

The moment things stopped working you would run the command git stash, checkout to main to see if everything is ok with it, then go back to your branch where you were working and run the command git stash pop. This command will bring up all the changes you were working on.

We can think of git stash as a temporary storage for a code that is not necessarily working, you leave it there and are free to checkout wherever you want, and when you go back to the branch, just give git stash pop that will give you back all that code.

Simple and valuable no? below is an example:

Here we are on the main branch, and this is the code that is running

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u6720fts8xkdzriauifl.png)

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8dev3xrumnmvybjv6xj.png)


At this moment I already created a new branch called feat/bad-code and added a line in it and it started to bring me a return that I didn’t expect

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qg72u8m0x0nl6a8iymg1.png)

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c9lom26jzbtwvy8qedbd.png)


To find out if this behavior is caused by code from the feat/bad-code branch, I’ll run the git stash command on the feat/bad-code branch, return to main, check that everything is fine, return to feat/bad-code and run git stash pop and my code will return

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/asqh3w96z39tfnuesun2.png)

My code after executing git stash

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gr0otpluwb4v3ewm0lam.png)

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hkdfcjaez761vusmepyw.png)


Now running git stash pop on the feat/bad-code branch to bring my code back

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9705madyg7df0o4dlorg.png)

Before git stash pop:

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6u2seyep5ocrwh87ye5.png)

After git stash pop:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/et3x2dic7xq8zdxcdx4r.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yb425vt928cc0or1533.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ntbac34yazqcu37pum3f.png)

❤ If the article was helpful to you in any way try to give ❤ and share it ❤