20

I have a patch that gives out the following output when I try to apply it with git am

Checking patch old/filename...
error: old/filename: does not exist in index

Within the patch old/filename is actually moved to new/filename but it seems the original file is already missing from the source tree.

So what is the error about and how to solve / work-around it? Can it just be ignored (with --reject or so)?

a1an
  • 2,914
  • 5
  • 32
  • 49

2 Answers2

8

The patch was not created against the correct source tree.

One way this could happen:

Assume your original branch (the one you want to apply the patch to) has commits:

  1. 1a -> 1b -> 1c -> 1d

This branch is then cloned, and new commits are made:

  1. 1a -> 1b -> 1c -> 1d -> 1e

Commit 1e included old/filename

Now you do the work in the patch, based on the second branch, not the original one:

  1. 1a -> 1b -> 1c -> 1d -> 1e -> 1f

Commit 1f included the rename old/filename -> new/filename

Now, if you create a patch for commit 1f, you won't be able to apply it on top of commit 1d, because commit 1e is missing where old/filename was added to the index/repository.

Jacques
  • 125
  • 1
  • 3
  • You can quick double check if the file that git is complaining about still exists. I ran into this issue just now, and the file git was complaining about had been actually moved by a co-worker of mine, and hence the issue. Git cherry-pick is smarter regarding such issues, and that worked great for me (you will have to do this commit by commit though). – user3613932 Apr 12 '17 at 21:51
2

You can use --reject to get it to do its best and output the rest in .rej files. Then you can fix it up manually and commit.

Tip extracted from: Raymes Khoury.

Felipe
  • 15,458
  • 9
  • 63
  • 87
  • --reject won't work with files not found. Only if the patch cannot be applied but file is found – Julien Dec 17 '20 at 10:49