Git stash: switch branches without losing your work

You are halfway through a feature and need to jump to another branch. Three commands keep your changes safe.

You are in the middle of a feature, the code is half written, and you need to switch branches. Git refuses to switch if your changes would be overwritten. You do not want to commit broken code either. git stash puts your unfinished changes on a shelf and gives you a clean working folder. The three commands 1. git stash saves your changes and cleans the folder. 2. git checkout build features moves you to the other branch. 3. git stash pop brings the changes back and removes them from the shelf. Give a stash a name When you have more than one stash, a message helps you find the right one later. The list shows each stash as stash@{0} , stash@{1} and so on, newest first. Bring one back without deleting it pop removes the stash after applying it. If you want to keep a copy on the shelf, use apply . Do not forget new files By default, git stash ignores files Git has never tracked. Add u to include them. If git stash pop reports a conflict, your changes are not lost. Fix the conflict markers in the files, then run git stash drop to remove the leftover stash. When I use it Before pulling the latest changes into a branch with unsaved work. When a quick bug fix has to go out while a feature is still unfinished. When I started coding on the wrong branch.