Git: save uncommitted changes in a new branch

If you want to commit your changes into a new branch, you can use git-stash.

Git-stash cuts all the changes you made till the last commit and keeps them in “the stash”. Later you can paste those changes from “the stash” somewhere else.

For example: you work on bug A on the master branch. Suddenly your boss comes in and demands an immediate fix for bug B. The fix for bug A isn’t complete yet, but you don’t want to loose the work you’ve already done.

This is the way to go:

Assume you’re on the master branch with uncommitted changes for bug A.

  • git stash => save all your changes in the stash. The master is now reverted to the last commit.
  • git branch bugfixA => make a branch for bugfix A
  • git checkout bugfixA => work on the bugfix A branch
  • git stash pop => “paste” the changes from the stash. Your files look now just like before.
  • git commit -a -m “debugging A” => commit the changes
  • git checkout master => now you’re on a clean master branch. Now you can choose. Either you create a new branch for bugfix B or you continue working on the master-branch.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.