Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do patches work in Git?

Tags:

git

patch

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.

like image 936
Pieter Avatar asked Jan 17 '10 19:01

Pieter


People also ask

How does a patch file work?

What is patch? patch is a command that takes the output from the diff and puts it into a file. Then, it can take the filed output and overwrite another file with with the changes. For example, a common use is to use the patch to transfer changes from the changed file to the original file, thus making them identical.

How do I git patch?

In order to create Git patch file for a specific commit, use the “git format-patch” command with the “-1” option and the commit SHA. In order to get the commit SHA, you have to use the “git log” command and look for the corresponding commit SHA.

How can I see my git patch?

Git Cola includes an "Apply Patches" dialog that can be launched from the Actions menu, or via the git cola am sub-command. You can open patches in this dialog and display the contents with diff syntax highlighting. This feature is available in master by cloning the repo and will be in the upcoming v3.

How do I apply a binary patch in git?

Using git apply provides the patch as unstaged changes in your branch. If you want to apply the patches as commits, you can use git am . $ git am rspec-changes. patch Applying: Add rspec to gemfile Applying: Add notes file $ git log --oneline ac9caff Add notes file f784b22 Add rspec to gemfile 8619310 ...


1 Answers

Create your patch via:

$ git format-patch master --stdout > patch.diff 

then patch.diff will contain the diff, which you can then send to someone else to apply using:

$ git am < patch.diff 

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

like image 62
Jon Avatar answered Sep 30 '22 23:09

Jon