39

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
  • What's preventing you from just restoring the stash, modifying it, and stashing it again. If you want to combine to stashes, just get both of them, and store it again. Commented Nov 9, 2011 at 4:22
  • 1
    @LeifAndersen: It's a pain when you have further changes that you don't want to add into the stash Commented Nov 9, 2011 at 4:27
  • Stash the changes you don't want in there first? Commented Nov 9, 2011 at 4:52
  • Isn't the difficulty that if you have (1) further changed a file that is modified in the stash, and (2) have not staged your further change, then git stash pop will 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) Commented Mar 1, 2024 at 16:54

4 Answers 4

25

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I am not sure why someone has down-voted this answer, as it seems to be valid... at least in most of the cases :)
Simple is better than complex. python.org/dev/peps/pep-0020/#id2
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. stackoverflow.com/a/8060523/7821547 has the right explanation for the mechanic OP is asking about.
20

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

7 Comments

@Casebash: I agree. Time for writing a patch to add this feature, but I suspect the problem can quickly becomes quite complex to manage.
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.
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 Nice. I guess I did not thought of that 5 years ago. I have included your comment in the answer for more visibility.
@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.
|
3

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.