Git Stash

Introduction

When I was a C developer for a company in Vietnam, I had to work with Git almost everyday. Our team was very active, we pushed commits to our gitlab server frequently. I am not a master of git, so I usually take a look at this visualization. It is a really helpful note for every software engineer working with Git. Sadly, our team worked only on the common branch “dev”, which caused a lot of troubles when we tried to push our update to the branch at the same time.

fig1

Another issue is “How can I update my colleague’s commit to my local repo while my local code has not been staged yet?”. Sometimes my colleague fixes a critical bug which I have to update immediately or it may affect my current developped feature. I look for a way to update the commit without hurting my code.

fig2

There are some resolution for 1st issue, such as merging or rebasing the remote branch to my local branch. However, the 2nd issue is a little bit tricky because I don’t want to commit the current working stage. My colleague showed me a useful git command, “git stash”, and its reversed version, “git stash pop”. Git stash will store your current working stage in a temporal space, which is a stack (last in - first out).

How I use this command

With this command, I can push my uncommitted code in a LIFO space, pull all the commits of my friends, then pop the uncommitted code back to my working stage. Now I only have to resolve the conflicts (if there is) and continue developping my current feature.

fig3

An alternative solution for 1st issue

Actually, “git rebase” is a combination of a set of git commands including “git stash”. A equivalent “git rebase” is as following illustration.

fig4

Note

  • We can stash and pop multiple times. Because stash space is a stack, what pushed first will be popped the last. We can check our stash list by the command “git stash list”.

  • We can delete an element in stash list by the command “git stash drop”. Be careful! After this command, your uncommitted modification may permanently removed.

  • Your uncommitted code must be only in working directory or stage. If the uncommitted code is in both of the working directory and the stage, git will not know which one should be stashed.