What to do if git-am fails with “does not exist in index”?
Asked Answered
M

4

25

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)?

Margotmargrave answered 1/10, 2013 at 12:9 Comment(0)
C
7

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.

Cataphyll answered 17/1, 2015 at 9:32 Comment(2)
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).Round
so as per question. what should he do then?Rase
W
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.

Willed answered 24/5, 2018 at 21:49 Comment(1)
--reject won't work with files not found. Only if the patch cannot be applied but file is foundWatercolor
B
2

'git am" does not give very clear location of failure. I did the following: Use "patch -p n patch_file" to try. This unix "patch" command shows clearly where the rejection happens. If patch command works, then the patch file is fine.

I saw "patch -p n" command worked, but "git am -p n" stilled failed with vague and confusing error message "does not exist in index". I suspected it just could not find the right file. So I tweaked n to n-1. Then it worked.

So, the actual cause is that -p arg should be fed with n-1, where n is the right one for the "patch" command.

In short, the working command is "git am -p n-1 patch_file".

Bartram answered 28/7, 2021 at 23:26 Comment(0)
D
1

Had a similar problem, applying the patch from one repo to another with different commits in between as described by Jacques: https://mcmap.net/q/55645/-what-to-do-if-git-am-fails-with-does-not-exist-in-index

Remark, the files to which I tried to apply the patch were not moved (same relative path). What helped was "using a three-way merge" as suggested here: https://mcmap.net/q/45990/-when-applying-a-patch-is-there-any-way-to-resolve-conflicts

git am -3 < changes.patch

Found it through Matt's comment on a similar problem: How can I generate a Git patch for a specific commit?

Dennett answered 2/12, 2022 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.