J
J
Jake Taylor2021-09-22 13:56:46
git
Jake Taylor, 2021-09-22 13:56:46

What is the fatal: Dirty index: cannot apply patches error in Git?

There are 2 branches (git_1 and git_2).
In the branch git_1:
1. I create a file 1.txtand add 2 lines to it. I make a commit.
2. I open this file again and add 2 more lines. I make a commit.
3. I create a file in the format mbox:
git format-patch -1 <hash-commit>

Next, I switch to the branch git_2and copy the file obtained in step 3 to this branch. Ok, he copied. I check it with the team git apply --check <file>and the check passes without errors.
Now I'm trying to apply the patch with the command git am <file>, but it gives me an error:

$ git am 0001-Step-7.patch
fatal: Dirty index: cannot apply patches (dirty: 0001-Step-7.patch)


Why?

Content of 0001-Step-7.patch :
From f9c90676efc6427d0c5db5ff9bb24e14e9e0cdb4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9Dn199a=E2=80=9D?= <”[email protected]”>
Date: Tue, 21 Sep 2021 21:44:28 +0300
Subject: [PATCH] Step 7

---
 module_1/task_2/1.txt | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 module_1/task_2/1.txt

diff --git a/module_1/task_2/1.txt b/module_1/task_2/1.txt
new file mode 100644
index 0000000..877d792
--- /dev/null
+++ b/module_1/task_2/1.txt
@@ -0,0 +1,2 @@
+Modify this line number 1 on step 7
+Modify this line number 2 on step 7
-- 
2.33.0.windows.2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jake Taylor, 2021-09-22
@n199a

It's decided.
When you execute the command:
git format-patch -1 <hash>
This command sets the name of the future file based on their line written in the commit. If you have a comment with dots in your commit, then the file will be created like "first_commit.1.word.patch" and because of these dots in the file name you will not be able to complete the git am command, this error will occur.
4 hours spent on this
Therefore, force the file name:

git format-patch -1 <hash> --stdout > file_name.patch

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question