Subhash
Subhash

Reputation: 178

Git keep staged changes

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

Upvotes: 5

Views: 4900

Answers (3)

dr. strange
dr. strange

Reputation: 665

# No need to stage
# put your current changes in stash
git stash

# create and checkout to b1 branch
git checkout -b b2

# resolve urgent issue and push changes 

# now get back to branch b1
git checkout b1

# Apply stashed changes back
git stash pop

Upvotes: -1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521093

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.

Upvotes: 3

knittl
knittl

Reputation: 265201

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

Upvotes: 3

Related Questions