Question

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.

Was it helpful?

Solution

I see a few simple steps to do:

  1. Move some files, only those that are required by a git stash show, back to old place:

    git mv file old_place/file
    
  2. Apply stash to those files:

    git stash pop
    
  3. Merge the changes, if any:

    vim ...
    git add ...
    
  4. 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.

OTHER TIPS

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top