Saturday, June 25, 2016

Unleashing the Git - part 5 - git stashing

More often than not, there can be situations where you have done some changes and you need to temporarily hide those from the working tree since those changes are not completely ready yet. Stashing comes into play in such situations. May be when you have done some changes in a branch and you want to switch to another branch without committing your changes in the current branch. Stash is the easiest option for that.

Say you have changed some files. Issuing the git status would show what has changed.
$ git stash // Temporarily hide your changes. Now issue git status. You won't see the modifications. Equivalent to $ git stash save
$ git stash save --patch // Opens the text editor to choose what portion to stash
$ git stash apply // Apply the last stashed change and keep the stash in stack. But this is normally not done. Instead following is done
$ git stash pop // Apply the last stashed change and remove it from stack
$ git stash list // Shows available stashes.
Stashes have name in the format of stash@{#} where # is 0 for most recent one and 1 for the second recent and so on.
$ git stash drop <stash name>   // e.g: git stash drop stash@{0} to remove the first stash
$ git stash clear // Remove all the stashed changes
$ git stash branch <stash name>  // Create a branch from an existing stash
In the next part of this series we'll discuss git branching in detail.

No comments:

Post a Comment