Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git keep staged changes

Tags:

git

github

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

like image 706
Subhash Avatar asked Jun 07 '16 08:06

Subhash


2 Answers

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
like image 183
knittl Avatar answered Oct 22 '22 03:10

knittl


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.

like image 39
Tim Biegeleisen Avatar answered Oct 22 '22 04:10

Tim Biegeleisen