echo "a" > file1.txt
git add file1.txt
git commit -m "add file1"
echo "b" > file1.txt
git stash
Now
echo "c" > file1.txt
git stash apply
Results in:
error: Your local changes to the following files would be overwritten by merge:
file1.txt
Please commit your changes or stash them before you merge.
Aborting
Cool! now let's stage file1.txt
and apply the stash again:
git add file1.txt
git stash apply
And we have a conflict:
Auto-merging file1.txt
CONFLICT (content): Merge conflict in file1.txt
What's the conflict (cat file1.txt
):
<<<<<<< Updated upstream
c
=======
b
>>>>>>> Stashed changes
In both cases, the stash was b
and file1.txt
in the working directory was c
.
So why git stash apply
was aborting when file1.txt
was not staged and then threw a conflict when it was staged?
Comprehensive responses are appreciated.
I read the whole stashing and cleaning chapter in the git book but couldn't make sense of why this happens.
The question is NOT how to fix this. Just wondering what happens behind the scenes in this scenario.