Git Stash
What is git stash and where to use it?
Imagine a situation where a developer is working on code changes and have made number of file changes in the code. You haven't tested these changes yet and thus you won't commit these changes to the local repository. And at this stage you are asked to work on a Hotfix. Now, if you move away from your current branch to a Hotfix branch, the current file changes will also appear in the Hotfix branch, which is what you don't want. So, to get rid of these file changes and set the Hotfix branch pointing to the HEAD commit, you can use the git stash command.
git stash command helps in saving the current state of your working directory and index while reverting the state to the HEAD commit. This is very helpful when we can not commit the indexed changes or the file changes in the working directory and also we can't discard such changes as well. So, stash helps in preserving such half baked changes in a temporary location and reverting the working directory to the current HEAD.
After we complete our work in hotfix, the saved current state then can be reverted back to the working directory from Stash with simple git stash apply command option again.
The command to get state of working directory and index into stash is,
$ git stash push -u
… this command gets applied to the files in work area and indexed changes. the -u switch helps in pushing untracked files as well. Once applied the files are moved to temporary storage area which git manages. and the working directory starts pointing to the HEAD.
One can also use the git stash without any sub-command added to it, but this command will simply add only your indexed changes to the stash, ignoring the untracked changes.
If we push a new stash again, then the latest stash is given stash id as stash@{0} and the earlier stash is moved to next element, i.e. stash@{1}
list and show option are used to get details about the stashed operations performed.
$ git stash list
… this will list all stashes as stash@{0}, stash@{1} ….
$ git stash show -p 1
This command shows details about a particular stash, i.e. stash@{1}
The latest stash created is stored in refs/stash. Older stashed are found in reflog of this reference and can be named using the usual reflog syntax (e.g. stash@{0} is the most recently created stash, stash@{1} is the one before it, stash@{2.hours.ago} is also possible).
After we are done with hotfix changes, as an example explained in here, we can get back the unfinished work from the stash back into the working directory of your local git repository.
$ git stash apply
The apply option with git stash command will get the files back into working directory.
To remove a stash element from the list we can use the drop command.
$ git stash drop
… The stash drop command option when run without any stash name, deletes the last stash, i.e. stash@{0} element.
If we want to delete a specific stash element from the listed stash, then supply the drop command with the stash element number, as shown below.
$ git stash drop 1
The above command will remove the stash@{1} from the saved stash list. The number after drop command word indicates the stash element number in the list.
If we want to assign the stash element a name, we can do that by using below command.
$ git stash save "describe it"
... gives the stash a name
$ git stash clear
... deletes all stashed commit (stashed entries / elements)
$ git stash save --keep-index
If we want to stash only unstaged files, i.e. files in the working directory only, excluding files that are indexed. This is exactly apposite to the push -u command option where we include unstaged files along with indexed files.
Comments