The tool for the job is git-stash. Running git stash
in a Git directory with changed files will stash away your changes, bringing the working directory back to the state of HEAD
(a clean state). This allows you to checkout a different branch, as well as many other operations. When you're done and back in this branch, run git stash pop
to pop the changes in the stash back onto the working directory, bringing yourself back the state you were at before stashing the changes away.
$ git branch
* develop-back
develop-other
$ git stash
$ git checkout develop-other
$ ...
$ git co develop-back
$ git stash pop
Yes, this works perfectly well and is very convenient. But remember that you shouldn't be afraid of committing changes in Git. In fact, git-stash does exactly that: commit your changes somewhere. Committing in Git means saving your work, and in this case that's exactly what you want to do. You can always reset-mixed:
$ git commit -am 'stashing away for a moment'
$ git co develop-other
$ ...
$ git co develop-back
$ git reset HEAD~
This workflow is equivalent to stashing, and you can see that it takes no more steps. git stash is just a "wrapper" around this procedure, with a somewhat more humane interface.