git
allows you to switch branches even if you have uncommitted changes in your current branch. The files remain uncommitted and you can commit them in the other branch if you need to. This is great when you realize that you've done work on feature/feature1
that should be on feature/feature2
.
Once you have committed the changes, they do not switch between branches any more. You then could merge or cherry pick changes between branches.
Note that git
will not allow you to switch branches if you have uncommitted changes that cause merge conflicts in the branch you want to switch to, and maybe even if one of your changes is in a file that has been changed and committed in the branch you want to switch to. In that case you need to commit, revert or stash your changes first.
If git
does not allow you to switch branches and you try to resolve that by stashing your changes before switching branches, you should make sure that you stash pop in the original branch. If you are on feature/feature1
, stash changes, then switch to feature/feature2
and stash pop there, you will loose changes that were present in feature/feature2
but not in feature/feature1
. This is hard to detect.