34

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?

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Casebash
  • 114,675
  • 90
  • 247
  • 350

4 Answers4

23

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}]

Ram Sinha
  • 241
  • 2
  • 3
  • 1
    I am not sure why someone has down-voted this answer, as it seems to be valid... at least in most of the cases :) – Maciek Talaska Feb 05 '16 at 01:21
  • `Simple is better than complex.` https://www.python.org/dev/peps/pep-0020/#id2 – Osama Dar Jan 24 '22 at 20:54
  • It's just not what op's question is. Supposed I have changed file A and file B and then stashed file A into stash 0. Then I continued working on file A such as it is now A'. I would then like to stash file B alongside the original file A. I don't want to unstash, effectively merging A into A'. It's a different set of operations. https://stackoverflow.com/a/8060523/7821547 has the right explanation for the mechanic OP is asking about. – Holy Jan 12 '23 at 18:10
19

I 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 tmp branch from the commit (HEAD) your are currently modifying
  • git stash pop twice
  • git stash, creating a new stash@{1} with both content in it,
  • deleting your temporary branch and checkouting the initial branch you where in
  • git stash pop once (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

  1. Create a new stash with whatever you have.
  2. 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 pop
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @Casebash: I agree. Time for writing a patch to add this feature, but I suspect the problem can quickly becomes quite complex to manage. – VonC Nov 09 '11 at 05:07
  • If you want some ideas, consider the TortoiseHg Workbench Shelve tool. Its workflow works really well for this kind of problem: Create shelves and add or remove stuff from them as you like from your working copy, or even between shelves. – Andre Luus Aug 30 '16 at 15:50
  • 5
    That's overcomplicated, I believe. The branching magic is completely unnecessary. You could simply 1. Create a new stash with whatever you have. 2. `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 `pop` – Powerslave Oct 21 '16 at 11:50
  • @Powerslave Nice. I guess I did not thought of that 5 years ago. I have included your comment in the answer for more visibility. – VonC Oct 21 '16 at 11:53
  • @VonC Nevertheless thank you for your answer! I was interested if there is any convenient way around and you pointed out just what I wanted to know. Forgot to hit +1, sorry, I'll follow up now. – Powerslave Oct 21 '16 at 13:10
  • when i attempt this...i get "Your local changes to the following files would be overwritten by merge - Aborting" – Tim Boland May 03 '17 at 22:52
  • @TimBoland It is best to ask a new question with specific details, to see why you get that message. – VonC May 04 '17 at 04:40
1

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.

hlovdal
  • 26,565
  • 10
  • 94
  • 165
1

You 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:

  1. Create the initial stash:
git stash save -u "initial stash"

  1. Make further changes that you want to add to the initial stash in your repository.

  1. Stage all the changes:
git add .

  1. List all the stashes available in your repository:
git stash list

You will see an output like this: stash@{0}: On main: initial stash


  1. Apply your initial stash on top of your additional changes:
git stash apply stash@{0}

  1. (Optional step) Resolve any conflicts that may arise between your initial stash and the current changes using your IDE.

  1. Create a new stash that combines your initial stash changes and the recent changes:
git stash save -u "initial stash combined with new changes"

  1. Apply your combined stash:
git stash apply stash@{0}

  1. 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.

Nivethan
  • 2,339
  • 19
  • 22