0

The use-case scenario is as follows:

git checkout branch-1
[introduce some changes I don't want to commit (but want to preserve)]
git stash save "my useful changes"

git checkout branch-2
[do some business with branch-2]

git checkout branch-1
git stash apply # the last stash which is "my useful changes"

Now, say I want to go back to branch-2 again. I am almost sure didn't introduce any new changes, so I don't want to create another stash if the changes are the same. I want to still have my stash with the same stash message.

I can manually check if the changes are the same (compare the diffs). And then either git reset / git stash; git stash drop in case they are indeed the same or create a new stash if they are not.

Is there a way to to this more automatically?

Solutions which are not good enough:

  • If I simply do git stash again, it creates a new stash (even if the contents and the message are identical to those of the top stash).
  • If I use git stash pop each time after I return to branch-1, I'll loose the stash message and so I'll have to re-type it each time.
  • If I do hard reset, I have to first check to make sure I didn't introduce any new changes other that changes in my top stash (so it's not automatic).

You could always say "write a script and it'll be automatic" and it's true. I want to know is there a feature integrated in git. So I consider a simple well-founded "No" a good answer.

Vladislav Ivanishin
  • 2,092
  • 16
  • 22
  • 2
    Maybe I'm not following your scenario properly, but I'd rather just create a branch and commit my changes to that branch, instead of stashing them. Then there's nothing to worry about (did I stash w/the right name, did I pop my changes). Given that you can create as many stashes or branches as you want, I see little benefit in using a stash when you think you might want to pop/push things from the stash multiple times. – Sunil D. Oct 19 '15 at 17:48
  • @SunilD., good point. I think, I'll go with creating new branches. The only downside is the additional branch (could be a little confusing if you have many branches). – Vladislav Ivanishin Oct 19 '15 at 17:53

1 Answers1

0

You can use git index for that.

Taking from where you left off:

$ git checkout branch-1
$ git stash apply # the last stash which is "my useful changes"
$ git add -A
# ... work on branch-1
$ git status

If there are any changes to the state you started with you will see some files as partially unstaged:

# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   <file>
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   <file>
#

If this is the case, then you need to update the stash. Otherwise, you can simply do a hard reset and continue with the existing stash.

Alternatively, you can add the changes to index before saving them to stash (with git stash -k) and apply the stash with git stash apply --index — this saves you one command after switching a branch.

eush77
  • 3,870
  • 1
  • 23
  • 30