ChatGPT解决这个技术问题 Extra ChatGPT

git stash -> merge stashed change with current changes

I made some changes to my branch and realized I forgot I had stashed some other necessary changes to said branch. What I want is a way to merge my stashed changes with the current changes.

Is there a way to do this?

Its more for convenience, I eventually gave up and committed first my current changes, then my stashed changes, but I would have preferred to get them in with one fell swoop.

Probably duplicate of stackoverflow.com/q/1360712/72178

M
Max Coplan

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.


Such a hack, but hey, it works and seems to be the only way to do this. I wish there was git stash apply --force or something.
Actually, it isn't a hack - it's an improvement over what you want, since you can easily revert to the changes in the index.
Wow, is this behaviour really intended by git ?
I don’t think there is anything ever “intended” by git. My hunch is by now that anything git does it does so by chance.
This is the perfect solution. I just did git add ., git stash apply, then git reset to apply the stash to my working changes and merge without having to make commits.
B
Brandan

Running git stash pop or git stash apply is essentially a merge. You shouldn't have needed to commit your current changes unless the files changed in the stash are also changed in the working copy, in which case you would've seen this error message:

error: Your local changes to the following files would be overwritten by merge:
       file.txt
Please, commit your changes or stash them before you can merge.
Aborting

In that case, you can't apply the stash to your current changes in one step. You can commit the changes, apply the stash, commit again, and squash those two commits using git rebase if you really don't want two commits, but that may be more trouble that it's worth.


I did get that message - the changes don't conflict but share the same files, any around using stashes/apply's?
Sorry, that's what I meant by "merge conflicts", but that was poor word choice. I think that error message is pretty final: if the files changed in the working copy are also changed in the stash, you can't apply the stash. I've updated my answer with a possible workaround.
I wouldn't consider this an answer in all cases. You might have stashed only part of a set of changes in a specific file because you wanted to test something out while developing. And you might not want to commit the current content of the file at this point in time (or not at all) as it's WIP. It's a real issue with git that stashed changes cannot be merged into your current branch
Joshua Warner's answer should be the one marked as correct. To merge a stash, stage your changes, apply the stash, deal with any conflicts, and then (if desired) unstage your changes.
"You can commit the changes, apply the stash, commit again, and squash those two commits using git rebase if you really don't want two commits, but that may be more trouble that it's worth." Instead of this you could do: Commit the changes, apply the stash, and then git commit --amend.
k
ks1322

What I want is a way to merge my stashed changes with the current changes

Here is another option to do it:

git stash show -p|git apply
git stash drop

git stash show -p will show the patch of last saved stash. git apply will apply it. After the merge is done, merged stash can be dropped with git stash drop.


Thanks for this--I don't know why git stash pop doesn't just do this in cases where the merge cleanly applies...
Extended version: git stash show -p --no-color | git apply --3way (--3way = fall back on 3-way merge if patch fails).
But git stash show -p creates a diff between the stashed contents and the commit back when the stash entry was first created. So this would overwrite the working file changes the OP made.
Why overwrite? The diff produced with git stash show -p will be merged by git apply, if it is possible to do without conflicts.
u
user3856437

The way I do this is to git add this first then git stash apply <stash code>. It's the most simple way.


How is this not an exact copy of the tl;dr of accepted answer?
k
knickum

As suggested by @Brandan, here's what I needed to do to get around

error: Your local changes to the following files would be overwritten by merge:
       file.txt
Please, commit your changes or stash them before you can merge.
Aborting

Follow this process:

git status  # local changes to `file`
git stash list  # further changes to `file` we want to merge
git commit -m "WIP" file
git stash pop
git commit -m "WIP2" file
git rebase -i HEAD^^  # I always use interactive rebase -- I'm sure you could do this in a single command with the simplicity of this process -- basically squash HEAD into HEAD^
# mark the second commit to squash into the first using your EDITOR
git reset HEAD^

And you'll be left with fully merged local changes to file, ready to do further work/cleanup or make a single good commit. Or, if you know the merged contents of file will be correct, you could write a fitting message and skip git reset HEAD^.


F
Frank-Rene Schäfer

May be, it is not the very worst idea to merge (via difftool) from ... yes ... a branch!

> current_branch=$(git status | head -n1 | cut -d' ' -f3)
> stash_branch="$current_branch-stash-$(date +%yy%mm%dd-%Hh%M)"
> git stash branch $stash_branch
> git checkout $current_branch
> git difftool $stash_branch

F
Finelf

you can easily

Commit your current changes Unstash your stash and resolve conflicts Commit changes from stash Soft reset to commit you are comming from (last correct commit)


C
Community

Another option is to do another "git stash" of the local uncommitted changes, then combine the two git stashes. Unfortunately git seems to not have a way to easily combine two stashes. So one option is to create two .diff files and apply them both--at lest its not an extra commit and doesn't involve a ten step process :|

how to: https://stackoverflow.com/a/9658688/32453


It turns the problem of applying one diff into a problem of applying two diffs. Additionally, the accepted solution doesn't involve a commit, just a stage, and it is only a single command (git add). (I'm not the downvoter.)
For me at least it feels simpler, less voodoo magic...cheers!