I am working on branch A and the feature/task is not done. Then I need to switch to another branch B for a quick fix. When I try to switch on another branch Git forces me to save my local changes otherwise I 'll lose my all local changes.
I need to commit my incomplete code. Is there any way that I can switch between multiple branches without committing and losing any code? Or is there a better way to handle the situation?
You can use git stash
, which will save your changes without creating a commit.1
First, stash your changes:
$ git stash
Then switch to your other branch:
$ git checkout branch-B
When you're ready, go back to your original branch and unstash your changes:
$ git checkout branch-A
$ git stash pop
See the documentation linked above for more details and specifics on additional use cases.
1 Technically it does create a commit, but git stash
uses some magic so you don't actually see the commit, and Git's tools know how to deal properly with these pseudo-commits.
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