Suppose that I have stashed some changes and I want to add further changes into the same stash (such as stash@{0}). Is there an easy way to do this? What about combining two stashes into a single one?
4 Answers
I don't think we have to follow all these steps,
git stash pop [this will apply your last stashed changes stash{@0}] then again say, git stash [this will create new stash will all the changes to stash@{0}]
NB: it is only this simple if your further changes don't require a merge with your stashed changes. If they do, you can git add . before stash pop or follow the more careful process in one of the other answers.
3 Comments
Simple is better than complex. python.org/dev/peps/pep-0020/#id2I don't see any "git stash" option allowing to modify an existing git stash.
A possible way to achieve this would be:
- stash your additional changes (
stash@{1}) - stash everything else (
stash@{2}) - create a
tmpbranch from the commit (HEAD) your are currently modifying git stash poptwicegit stash, creating a newstash@{1}with both content in it,- deleting your temporary branch and checkouting the initial branch you where in
git stash poponce (to restore all the pending changes)- continue your selective stash
Five years later, Powerslave proposes in the comments:
The branching magic is completely unnecessary.
You could simply
- Create a new stash with whatever you have.
git stash applyboth changesets (you cangit stash popinstead, but in that case you're in trouble if you accidentally screw up).- Create a new stash with these merged changes.
git stash dropthe other two changesets if you usedapplyinstead ofpop
7 Comments
git stash apply both changesets (you can git stash pop instead, but in that case you're in trouble if you accidentally screw up). 3. Create a new stash with these merged changes. 4. git stash drop the other two changesets if you used apply instead of popYou cannot add new changes to an old stash, but you can combine and create a new stash that includes both your current changes and the old stash. Here's how you can do it:
- Create the initial stash:
git stash save -u "initial stash" - Make further changes that you want to add to the initial stash in your repository.
- Stage all the changes:
git add . - List all the stashes available in your repository:
git stash list You will see an output like this: stash@{0}: On main: initial stash
- Apply your initial stash on top of your additional changes:
git stash apply stash@{0} - (Optional step) Resolve any conflicts that may arise between your initial stash and the current changes using your IDE.
- Create a new stash that combines your initial stash changes and the recent changes:
git stash save -u "initial stash combined with new changes" - Apply your combined stash:
git stash apply stash@{0} - Delete your initial stash:
git stash drop 1 You will see an output like this: Dropped refs/stash@{1}
By following these steps, you can effectively combine your initial stash with new changes and continue your work.
Comments
An old question, but I feel it still is missing the right answer: Do not use git stash, use temporary commits and/or temporary branches.
Suppose that I have stashed some changes and I want to add further changes into the same stash (such as stash@{0}). Is there an easy way to do this?
Yes, when you use normal, uncomplicated commits, this is just a normal interactive rebase with an edit action.
What about combining two stashes into a single one?
When you use normal, uncomplicated commits, this is just a normal interactive rebase with a fixup action.
git stash popwill fail with the unhelpful message "error: Your local changes to the following files would be overwritten by merge: ... Please commit your changes or stash them before you merge" whereas "… Please add your changes to the index before popping" might give us a clue? (Although, you still have to reconcile your new change with your stashed change)