My current branch has moved certain files to different folders, and the stash created had made changes to the files which were located in the old folders.
That is typical of git stash trying to detect moved/renamed files.
That won't be an issue anymore with Git 2.12 (Q1 2017):
See commit 9d4e28e (06 Dec 2016) by Jeff King (peff
).
(Merged by Junio C Hamano -- gitster
-- in commit db09f21, 19 Dec 2016)
stash
: prefer plumbing over git-diff
:
When diff.renames
configuration is on (and with Git 2.9 and later,
it is enabled by default, which made it worse), "git stash
" misbehaved if a file is removed and another file with a very similar content is added.
When creating a stash, we need to look at the diff between the working tree and HEAD, and do so using the git-diff porcelain.
Because git-diff
enables porcelain config like renames by default, this causes at least one problem. The --name-only
format will not mention the source side of a rename, meaning we will fail to stash a deletion that is
part of a rename.
We could fix that case by passing --no-renames
, but this is a symptom of a larger problem. We should be using the diff-index
plumbing here, which does not have renames enabled by default, and also does not respect any
potentially confusing config options.
That means the following will work:
mv file renamed &&
git add renamed &&
git stash &&
git stash apply
What will be restored is a file named 'renamed
', not a file named 'file
' (which was moved as 'renamed
' before git stash
)