jerry
jerry

Reputation: 1261

git how to apply patch generated from git diff --no-index /backup /source

i'm working on a large project, which consists of many git repos, and also bunch of files/dirs not tracked by git. i'm using sed -i.bak to apply mass changes, which created lots of .bak files

to avoid modifying files/dirs not tracked by git, i created a backup folder somewhere else, moved all .bak files to backup folder, rename them to original file names

i created big.patch by git diff --no-index backup/file source/file to all files in backup folder. i believe in this way the patch is complete, which should include files tracked by git and files not tracked by git

update: when generating patch file, there were many warning messages: warning: CRLF will be replaced by LF in /path/to/source/filename.

i'm planning to revert back the source folder from backup folder, and just keep the big patch file

how do i apply this big patch file which includes patches for multiple git repos and files/folders not tracked by git, to source folder?

i've tried git apply --check big.patch, there's error like

error: git diff header lacks filename information when removing 1 leading pathname component (line 34581)

patch content around line 34581

diff --git a/path/to/backup/1.html b/path/to/source/1.html
index 109deee..7e41088 100644
--- a/path/to/backup/1.html
+++ b/path/to/source/1.html
@@ -5,7 +5,7 @@ Do Not Edit! -->
 <TITLE>Bookmarks</TITLE>
 <H1>Bookmarks</H1>
 <DL><p>
-    <DT><A HREF="https://www.google.com/" ADD_DATE="1424779391" LAST_VISIT="1424779391" LAST_MODIFIED="1424779392" ICON_URI="https://www.google.com/favicon.ico" >Google</A>
+    <DT><A HREF="https://www.notgoogle.com/" ADD_DATE="1424779391" LAST_VISIT="1424779391" LAST_MODIFIED="1424779392" ICON_URI="https://www.google.com/favicon.ico" >Google</A>
     <DT><A HREF="https://login.live.com/" ADD_DATE="1424779398" LAST_VISIT="1424779398" LAST_MODIFIED="1424779398" ICON_URI="https://auth.gfx.ms/16.000.25462.00/favicon.ico?v=2" >Outlook</A>
     <DT><A HREF="http://www.speedtest.net/" ADD_DATE="1424779406" LAST_VISIT="1424779406" LAST_MODIFIED="1424779406" ICON_URI="http://www.speedtest.net/favicon.ico" >Speed Test</A>
 </DL><p>
diff --git a/some/other/file ....

line#34581 is <TITLE>Bookmarks</TITLE>

updated per @hackape 's solution:

after changing path in patch file, new error message:

error: patch fragment without header at line 243725: @@ -184,7 +184,7 @@ Error<BR>

patch content near line 243725

diff --git a/source/1.txt b/source/1.txt
index d306c79..51024d7 100644
--- a/source/1.txt
+++ b/source/1.txt
@@ -1311,7 +1311,7 @@ Tests DOMSnapshot.getSnapshot method.
             attributes : [
                 [0] : {
                     name : href
-                    value : https://www.google.com/
+                    value : https://www.notgoogle.com/
                 }
             ]
             backendNodeId : <number>

line 243725 is name : href

Upvotes: 1

Views: 2192

Answers (1)

hackape
hackape

Reputation: 19947

IIUIC, you made changes right in source/ files but also keep a backup/ snapshot, now you want to restore source/ to their original state before changes.

Here's a simple git setup to demonstrate:

.
├── backup
│   └── foo.txt # bar
└── source
    └── foo.txt # zoo

So original content of source/foo.txt is bar, now changed to zoo. backup/foo.txt is still bar.

First run git diff --no-index backup/* source/* > big.patch and get the patch:

diff --git a/backup/foo.txt b/source/foo.txt
index 5716ca5..d15dc45 100644
--- a/backup/foo.txt
+++ b/source/foo.txt
@@ -1 +1 @@
-bar
+zoo

Second replace all a/backup occurrence in big.patch to a/source, we got:

diff --git a/source/foo.txt b/source/foo.txt
index 5716ca5..d15dc45 100644
--- a/source/foo.txt
+++ b/source/foo.txt
@@ -1 +1 @@
-bar
+zoo

Last reverse apply the patch with git apply big.patch --reverse. This will restore source/ back to original state, identical to backup/. And if you run git apply big.patch at this point, it will apply the mass changes.

Upvotes: 2

Related Questions