{"thread":{"id":"53432","subject":"git apply -3 doesn't work","startedAt":"2020-05-08T10:35:56Z","lastAt":"2020-05-08T13:59:14Z","messageCount":3,"participants":["Konstantin Kharlamov","Phillip Wood"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"397398","messageId":"d9d549eb-e888-c930-1aeb-38cd3aad1b3b@yandex.ru","threadId":"53432","inReplyTo":null,"subject":"git apply -3 doesn't work","fromName":"Konstantin Kharlamov","fromEmail":"hi-angel@yandex.ru","sentAt":"2020-05-08T10:35:42Z","receivedAt":"2020-05-08T10:35:56Z","isPatch":false,"sender":{"key":"hi-angel@yandex.ru","avatar":null},"body":"As description says. To check that the 3-way merge should actually work subsitute `git apply -3` with `git am -3` in the following steps. This way it works.\n\n# Steps to reproduce\n\n     $ git init\n     Initialized empty Git repository in /tmp/foo/.git/\n     $ echo hello > file1 && git add file1 && git commit -m \"initial commit\"\n     [master (root-commit) 8334093] initial commit\n      1 file changed, 1 insertion(+)\n      create mode 100644 file1\n     $ git checkout -b mybranch\n     Switched to a new branch 'mybranch'\n     $ echo bye > file1 && git add file1 && git commit -m \"change file1 text\"\n     [mybranch 1807900] change file1 text\n      1 file changed, 1 insertion(+), 1 deletion(-)\n     $ git format-patch -1 --stdout > 1.patch\n     $ git checkout master\n     Switched to branch 'master'\n     $ mv file1 file2\n     $ git add -u && git add file2 && git commit -m \"renamed file1 to file2\"\n     $ git apply -3 1.patch\n     error: file1: does not exist in index\n\n## Expected\n\nGit apply successfully returns\n\n## Actual\n\nIt prints\n\n\terror: file1: does not exist in index\n\n# Versions\n\ngit 2.26.2\n"},{"id":"397404","messageId":"bf5fab9b-78f0-8c8c-dcf6-e6672014412f@gmail.com","threadId":"53432","inReplyTo":"d9d549eb-e888-c930-1aeb-38cd3aad1b3b@yandex.ru","subject":"Re: git apply -3 doesn't work","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2020-05-08T13:47:33Z","receivedAt":"2020-05-08T13:47:44Z","isPatch":false,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Konstantin\n\nOn 08/05/2020 11:35, Konstantin Kharlamov wrote:\n> As description says. To check that the 3-way merge should actually work \n> subsitute `git apply -3` with `git am -3` in the following steps. This \n> way it works.\n> \n> # Steps to reproduce\n> \n>      $ git init\n>      Initialized empty Git repository in /tmp/foo/.git/\n>      $ echo hello > file1 && git add file1 && git commit -m \"initial \n> commit\"\n>      [master (root-commit) 8334093] initial commit\n>       1 file changed, 1 insertion(+)\n>       create mode 100644 file1\n>      $ git checkout -b mybranch\n>      Switched to a new branch 'mybranch'\n>      $ echo bye > file1 && git add file1 && git commit -m \"change file1 \n> text\"\n>      [mybranch 1807900] change file1 text\n>       1 file changed, 1 insertion(+), 1 deletion(-)\n>      $ git format-patch -1 --stdout > 1.patch\n>      $ git checkout master\n>      Switched to branch 'master'\n>      $ mv file1 file2\n>      $ git add -u && git add file2 && git commit -m \"renamed file1 to \n> file2\"\n>      $ git apply -3 1.patch\n>      error: file1: does not exist in index\n\nJudging from the error message it never gets as far as trying the 3 way \nmerge because it first tries to apply the patch which modifies file1 but \nfile1 does not exist in the index. If it were to try a 3 way merge in \nthis case then on one side of the merge you've modified file1 and on the \nother side it has been deleted so there would be a merge conflict. The \nreason 'git am' succeeds is that the rename detection kicks in and so it \napplies the patch to file2 rather than file1\n\nBest Wishes\n\nPhillip\n\n> \n> ## Expected\n> \n> Git apply successfully returns\n> \n> ## Actual\n> \n> It prints\n> \n>      error: file1: does not exist in index\n> \n> # Versions\n> \n> git 2.26.2\n"},{"id":"397405","messageId":"f5e4a133-8cfc-0f6f-bfeb-b62acca5d110@yandex.ru","threadId":"53432","inReplyTo":"bf5fab9b-78f0-8c8c-dcf6-e6672014412f@gmail.com","subject":"Re: git apply -3 doesn't work","fromName":"Konstantin Kharlamov","fromEmail":"hi-angel@yandex.ru","sentAt":"2020-05-08T13:52:44Z","receivedAt":"2020-05-08T13:59:14Z","isPatch":false,"sender":{"key":"hi-angel@yandex.ru","avatar":null},"body":"On 5/8/20 4:47 PM, Phillip Wood wrote:\n> Hi Konstantin\n> \n> On 08/05/2020 11:35, Konstantin Kharlamov wrote:\n>> As description says. To check that the 3-way merge should actually work subsitute `git apply -3` with `git am -3` in the following steps. This way it works.\n>>\n>> # Steps to reproduce\n>>\n>>      $ git init\n>>      Initialized empty Git repository in /tmp/foo/.git/\n>>      $ echo hello > file1 && git add file1 && git commit -m \"initial commit\"\n>>      [master (root-commit) 8334093] initial commit\n>>       1 file changed, 1 insertion(+)\n>>       create mode 100644 file1\n>>      $ git checkout -b mybranch\n>>      Switched to a new branch 'mybranch'\n>>      $ echo bye > file1 && git add file1 && git commit -m \"change file1 text\"\n>>      [mybranch 1807900] change file1 text\n>>       1 file changed, 1 insertion(+), 1 deletion(-)\n>>      $ git format-patch -1 --stdout > 1.patch\n>>      $ git checkout master\n>>      Switched to branch 'master'\n>>      $ mv file1 file2\n>>      $ git add -u && git add file2 && git commit -m \"renamed file1 to file2\"\n>>      $ git apply -3 1.patch\n>>      error: file1: does not exist in index\n> \n> Judging from the error message it never gets as far as trying the 3 way merge because it first tries to apply the patch which modifies file1 but file1 does not exist in the index. If it were to try a 3 way merge in this case then on one side of the merge you've modified file1 and on the other side it has been deleted so there would be a merge conflict. The reason 'git am' succeeds is that the rename detection kicks in and so it applies the patch to file2 rather than file1\n> \n> Best Wishes\n> \n> Phillip\n> \n\nThank you. I wonder if it would make sense to `git aplly -3 1.patch` to behave similarly to `git am -3 1.patch && git reset HEAD^` (except without modifying reflog). I.e. so all heuristics `git am` makes use of would also apply to `git apply`\n"}]}