Thursday, January 2, 2014

Git stash

Using git stash

Sometimes we are working on a new feature on a project using the git version control and we are requested to fix a bug or just check some piece of code on another branch. We would not want to commit a half way done feature, also we would not like to delete the changes to the branch. Well we can use git stash, by just running this command the changes are stashed until we later need them, if we run git status we can see that no changes appear to the branch and we can change branches without the need to commit unstable code and messing our project history. After we finished fixing the bug we checkout the original branch at which we started the feature and we can run git stash apply to add back the changes to the branch.

Other scenarios

There are other scenarios when we might use git stash. For example we started to implement a new feature on a wrong branch. Again git stash helps us, we can run this command and create a new branch using git checkout -b some_new_branch or on an existing branch using git checkout some_existing_branch, now we can run git stash apply and the code which was not commit on the previous branch appears now on the new one.

We can also stash multiple set of changes and you can see them using git stash list, if we want to apply changes from a specific stash we can do git stash apply stash@{stash number}