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
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"
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
遵循以下流程:
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^