Federico Mengozzi

Git's stack container for temporary changes

Stashing changes with Git

Move changes between different branches

When working on projects, sometimes, I found myself making changes in the wrong branch. Fortunately I discovered another amazing command in Git: git stash.

This command save all local changes to a stack-like container and reset the current branch to HEAD. A more verbose use would be git stash push -m "message", in this way it’s possible to add a message describing what the stash is about.

To see what has been stashed so far the command git stash list will list all the entry.

When I need to move changes to another branch I usually stash the changes, move to the target branch and then git stash apply to complete the job. Once applied, the stashed change are still stored, to remove the latest entry in stack I use git stash drop (git stash pop for automatically applying and removing a stash). To remove all stashed entry just use git stash clear.

The command git stash apply will apply the latest stashed modifications. To apply a specific stash, let’s say the third stash, run git stash apply stash@{2} (stashes are store in a stack, with indices starting from 0 with the older stash having index 0).

Another useful trick that can be performed using this feature is checking the differences between the local files and the stashed changes, to do that use git stash show (again it’s possible to select which stash enquire).

Go to top