Git: switching branches

If you’re modifying files in the working directory and you find out you need to fix a bug on a different branch. You can’t just switch to a different branch and lose all your work. At this point you can use git stash.

Git stash saves your working directory and index to a safe place and restores your working directory and index to the most recent commit. I.e. the working directory is set back to its state before the modifications were made. You can then work on another branch and when you’re ready to get back to where you were, you type git stash pop and you’re back.

Before you start git stashing, make sure any new files added to the working directory have been added to the index: git stash will not stash (save) files in the working directory unless the files are being tracked (some version of the file has been added to the index with the git add command).

More information can be found on GitGuys.

Leave a Reply