问题
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. How to properly apply the stash without breaking anything, and would like to know the easiest method for this.
回答1:
I see a few simple steps to do:
Move some files, only those that are required by a
git stash show, back to old place:git mv file old_place/fileApply stash to those files:
git stash popMerge the changes, if any:
vim ... git add ...Move files back to the proper places:
git mv old_place/file file
After that you will get the added into index files with applied stash.
回答2:
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 overgit-diff:When
diff.renamesconfiguration 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.
Becausegit-diffenables porcelain config like renames by default, this causes at least one problem. The--name-onlyformat 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 thediff-indexplumbing 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)
来源:https://stackoverflow.com/questions/21280109/applying-stash-changes-in-git-where-the-stash-contains-files-that-has-been-move