How do I keep my staged changes without committing and work on other issue's where these changes are not required ?
I have created a branch say b1 and made some changes and staged that changes. Now I had an urgent issue which I need to work on but I don't require the staged changes here. So I switched to branch b2 and fetched and pulled from upstream. But the staged changes still exists in the b2 branch
Do not stage, but stash the changes. You can then later apply them again to your working copy:
git stash
git checkout -b urgent-issue
git commit
git checkout -
git stash pop
An alternative to the answer given by @knittl is to actually go ahead and make a temporary commit (keep reading):
git commit -m 'WIP'
git checkout urgent-issue
Once you have committed your work, your working directory will be clean and you can switch branches as you need. When you want to return to your current branch, you can continue the work and amend the temporary commit you made.
git checkout feature # return to original branch
# work work ... complete the commit
git commit --amend -m 'Finished the work I started earlier'
I usually prefer making a temporary commit over a stash when I have critical work for several reasons. First, it eliminates the need to have to sort through the stash stack looking for the stash I want to apply (and it easy to let the stack pile up with old junk). Second, because I know that I have a commit with unfinished work, it forces discipline on me to complete the feature.
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