'Creating a patch file from a diff of 2 folders
I made some changes to an open source project without taking time to create proper patch files.
Now, the maintainer of the project released a new version, and among new and edited files, there are a bunch of renamed files.
What is the best way to apply my changes to the new version ?
I'm completely new to diff/patch use, and If I can get it done with git, it would be better.
Solution 1:[1]
If the project is under git and you haven't committed your changes locally, you can simply do git diff > file.patch to get patchable diff data. If you have committed the changes locally, you can do git log to find the commit before you and than git diff commit_string > file.patch.
If the project isn't under git, or if you d/l source without cloning the repository (as the title suggests), you can use diff -urN original_dir new_dir > file.patch to create the patch file. In both cases you can try use patch later to apply the patch.
However, consider using git tools to merge your changes with the new version, since git is able to track filename changes too. You'll need to learn great deal about git itself, and it will take you some time to get it right - you should probably backup your work before starting to play with it.
Solution 2:[2]
Git has support for detecting renaming of files, so if you are lucky it will help you with that. The following is an approximate draft of what you should do.
Import the original version:
tar zxvf open-source-project-0.1.tar.gz
mv open-source-project-0.1 open-source-project
cd open-source-project
git init
git add .
git commit -m "Initial checkin of open-source-project-0.1"
git tag open-source-project-0.1
Now you can apply your original changes in a separate branch:
git checkout -b mychanges
cp /somewhere/where/your/changes/files/are/* .
git diff
git add .
git commit -m "My changes"
git tag my_changes_001
Then you update to the newer version:
git checkout master
tar zxvf open-source-project-0.2.tar.gz
mv open-source-project-0.2/* .
rmdir open-source-project-0.2
git add .
git commit -m "Update to open-source-project-0.2"
git tag open-source-project-0.2
So far everything is checked in into the git repository, now is the time to start to try to merge your changes:
git checkout -b merge_test open-source-project-0.2
git pull . my_changes_001
Good luck...
If you want to merge files manually I really recommend using KDiff3. Assuming file1.c is from open-source-project-0.1, file2.c from open-source-project-0.2 and file3.c from your changes, run
kdiff3 -o merged_file.c file1.c file2.c file3.c
Solution 3:[3]
I couldnt comment due to low reputation. So I will add a new answer. I tried one of the answers above still had issues with blank lines. Below method worked for me though.
diff -uraBN upstream local > filename.patch
cd upstream
patch --dry-run -p1 -i filename.patch #highly recommended recommended
patch -p1 -i filename.patch
Solution 4:[4]
You could try something that was suggested to me here before, an interesting "diffing" solution: first clone the latest version of the project. It shouldn't have any local changes. Make sure the .git folder is there. Then copy your working tree, that is all your files EXCEPT the .git folder, to the cloned repo. Now if you type "git st" you will see everything that was changed. You'll need to sort out also the spacing and line endings (git config core.autocrlf ...) if git st reports files that don't really have changes in them.
Now for each file in git st list, if you type git diff you'll see your changes.
Then I would edit the files one by one, until git st looks like what I want to commit.
I wouldn't rely on making patches because they are extremely picky. You'll most likely get a "can't apply patch", whereas the solution above gives you a list of unstaged changes which you can work on in any order you want.
Solution 5:[5]
Folder 1:
index1.html
css/style.css
js/
Folder 2:
index1.html
index2.html
css/style.css
js/
Now we want to take diff of two folders html1 and html2 directories. In html1 the CSS is changed.
In html2 another index2.html is added. Now, we want to take diff from html2 to html1
$ git diff html2/ html1/ > patches/htmlchanges.patch
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | niry |
| Solution 2 | hlovdal |
| Solution 3 | Aj700 |
| Solution 4 | |
| Solution 5 | Paul Bradbury |
