DEV Community

Tohid Heshmati
Tohid Heshmati

Posted on • Updated on

git stash for dummy’s

git stash for dummy’s or dirty & temporary commit

I strongly believe in learning the core of something with simple steps and then broaden the knowledge by exploring options.

I encountered "git stash" just yesterday and here is the simple steps towards it and.

Scenario: I have worked on a task and in middle of it read on team slack that

the develop branch has been updated. please update your branches

But I'm still in middle of my work. I have written lines of codes on my own branch. I don't want to commit or delete my code. Deleting is waste of time. If I do commit, it would be a bad commit because I'm not sure about my work. But I want my develop branch to be updated. what should I do?

The way to go is STASH. by stashing we save our dirty work and easily switch to other branches, update them and get back to our work.

lets call it dirty & temporary commit.

first thing to do is to do this dirty commit. instead of normal git commit message this is the command.

git stash
Enter fullscreen mode Exit fullscreen mode

to have a message like commit message we can choose save option like below.

git stash save "message"
Enter fullscreen mode Exit fullscreen mode

now we are free to go to other branches and do any kind of update. in my case: checking out develop branch + pulling from origin + merging it to my Task branch:

git checkout develop

git pull origin develop

git checkout my_Task_Branch_Name

git merge develop
Enter fullscreen mode Exit fullscreen mode

now our project is up to date. but we miss codes that we have written. to see all the stashed (or our little secrete ;) temporary committed)

git stash list 
Enter fullscreen mode Exit fullscreen mode

you will see one or more stash. it is strongly recommended not to have more than one stash.
anyway you need your code back, to bring back your stashed code you have to apply it using this command.

git stash apply stash@{number}
Enter fullscreen mode Exit fullscreen mode

so far you are done. congrats. you are both updated and have your code back. if the list of stashes bothers you as it bothers me try the pop option to apply and delete.

git stash pop stash@{number}
Enter fullscreen mode Exit fullscreen mode

or to clear all of it

git stash clear
Enter fullscreen mode Exit fullscreen mode


git

Also it's good to note that by using drop option you can delete specific or all of your git stashed codes. One may say that pop option is much better because it applies and drops at the same time. So pop is somehow the combination of those two. It can be good but I'm personally not fan of it. It might cause you problems. At least at beginning it's good Idea to use apply & drop separately.

you may also encounter ~WIP~ a lot and is good to know that it stands for ~Work In Progress~.

to see more options and possibilities check here

Top comments (0)