Git - Stash Operation

Advertisements


Suppose you are implementing a new feature for your product. Your code is in progress and suddenly customer escalation comes. Because of this, you have to keep aside your new feature work for few hours. You cannot commit your partial code and also cannot throw away your changes. So you need some temporary space, where you can store your partial changes and later on commit it.

In Git, stash operation takes your modified tracked files and stage changes and saves it on a stack of unfinished changes that you can reapply at any time.

[jerry@CentOS project]$ git status -s
M string.c
?? string

Now, you want to switch branches for customer escalation, but you don’t want to commit what you’ve been working on yet; so you’ll stash the changes. To push a new stash onto your stack, run git stash command.

[jerry@CentOS project]$ git stash
Saved working directory and index state WIP on master: e86f062 Added my_strcpy function
HEAD is now at e86f062 Added my_strcpy function

Now, you’re working directory is clean and all changes are saved on stack. Let us verify it with git status command.

[jerry@CentOS project]$ git status -s
?? string

Now you can safely switch the branch and do work elsewhere. We can view list of stashed changes by using git stash list command.

[jerry@CentOS project]$ git stash list
stash@{0}: WIP on master: e86f062 Added my_strcpy function

Suppose you resolved customer escalation and you are back on your new feature and you want your half-done code, just execute git stash pop command, it will remove changes from stack and place it in the current working directory.

[jerry@CentOS project]$ git status -s
?? string

[jerry@CentOS project]$ git stash pop

Above command will produce the following result:

# On branch master
# Changed but not updated:
# (use "git add ..." to update what will be committed)
# (use "git checkout -- ..." to discard changes in working directory)
#
#
modified: string.c
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
#
string
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (36f79dfedae4ac20e2e8558830154bd6315e72d4)

[jerry@CentOS project]$ git status -s
M string.c
?? string


Advertisements
Advertisements