Merge stashed change with current changes

git, git-merge, git-stash

Solution

tl;dr

Run `git add` first.

I just discovered that if your uncommitted changes are added to the index (i.e. "staged", using `git add ...`), then `git stash apply` (and, presumably, `git stash pop`) will actually do a proper merge. If there are no conflicts, you're golden. If not, resolve them as usual with `git mergetool`, or manually with an editor.

To be clear, this is the process I'm talking about:

mkdir test-repo && cd test-repo && git init
echo test > test.txt
git add test.txt && git commit -m "Initial version"

# here's the interesting part:

# make a local change and stash it:
echo test2 > test.txt
git stash

# make a different local change:
echo test3 > test.txt

# try to apply the previous changes:
git stash apply
# git complains "Cannot apply to a dirty working tree, please stage your changes"

# add "test3" changes to the index, then re-try the stash:
git add test.txt
git stash apply
# git says: "Auto-merging test.txt"
# git says: "CONFLICT (content): Merge conflict in test.txt"

... which is probably what you're looking for.

Problem

I made some changes to my branch and realized I forgot I had stashed some other necessary changes to the said branch. What I want is a way to merge my stashed changes with the current changes. Is there a way to do this? It’s more for convenience, I eventually gave up and committed my current changes first, then my stashed changes, but I would have preferred to get them in with one fell swoop.

Original source

Related problems