I'm new to Git, but familiar with SVN. As a test I made a repository in a local directory with git init
. Then I cloned the empty repository (over SSH using 127.0.0.1, which is another thing I wanted to test) to another local directory. I added some files in repository 2, I did git add *
and finally git commit -a -m "First source code"
.
I now want to create a patch using git format-patch
and apply it on repository 1. How do I do this? I know there's a manual, but these things are terribly complicated and make me wanna do certain things to my monitor.
Create your patch via:
then patch.diff will contain the diff, which you can then send to someone else to apply using:
Sometimes, when the manuals are a little dense, it makes sense to look for a tutorial:
http://luhman.org/blog/2009/09/22/git-patch-tutorial
Using GitHub patch
Add
.patch
to a commit URL to get the patch file, examplegithub.com/git/git/commit/b6b3b6a.patch
Patch the original file like this:
Using GitHub diff
Add
.diff
to a commit URL to get the patch file, examplegithub.com/git/git/commit/b6b3b6a.diff
Patch the original file like this:
§5.3 Distributed Git - Maintaining a Project
You have to go to "repository 2", the one you want to create the patch from, and run git-format-patch to create the patch : git format-patch master --stdout > name_of_patch_file
Then you go in "repository 1", the one you want to apply the patch to : git apply name_of_patch_file
Sometimes it is useful to just check if the patch will cause problems : git apply --check name_of_patch_file
The proper and easier way to do this if you're using Git is via remotes:
This will migrate commits from one repo to another, including their authors and commit messages, and will help you sort out merge conflicts (especially if you're moving > 1 commit)
The easiest method to create patches from the last commit (or last few commits) is to use
format-patch
with a negative number indicating the number of commits to create patches for:You'll get a patch file named after the commit description. The use
am
to insert it into another repository: