I am working on a feature branch and have not finished the work there - Now I need to change to a different branch to fix something
for example
feat1 - 6+ files changed
When I checkout to feat2 branch, after git add .
in feat1, git seems to carry over the staged yet uncommitted file changes.
If I commit these file changes in feat1, checking out to feat2 will not carry over those changes
How can I switch branches without committing file changes?
Warning You should commit all of your current changes before switching branches. If you have uncommitted changes when you switch branches, they will be lost.
If you have modifications to a file that is identical between two branches, switch from one branch to the other will not require a stash. If the file is different on your other branch, Git will not let you switch branches, as this would destroy your uncomitted changes.
Stash them:
$ git stash save -u "Some logical description of the changes"
$ git stash list
stash@{0}: Some logical description of the changes
$ git checkout other-branch
When you're done, you can use git stash apply
to apply the changes in your stash and keep the stash around, or git stash pop
to apply the changes and remove the stash as long as there are no conflicts.
If you end up with multiple stashes at once you can apply
or pop
them in an arbitrary order using the stash@
string, e.g. git stash apply stash@{6}
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With