How do patches work in Git?

被刻印的时光 ゝ 提交于 2019-12-18 10:22:38

问题


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.


回答1:


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




回答2:


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:

git format-patch -1

You'll get a patch file named after the commit description. The use am to insert it into another repository:

git am << name_of_patch_file



回答3:


The proper and easier way to do this if you're using Git is via remotes:

cd \path\to\repo1
git remote add otherrepo \path\to\repo2
git fetch otherrepo

git log otherrepo/master  ## Find the commit you want to steal in the list

git cherry-pick SOME_SHA1  ## Snag just one commit
git merge otherrepo/master  ## Merge all of the new commits from otherrepo/master

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)




回答4:


Using GitHub patch

  1. Add .patch to a commit URL to get the patch file, example

    github.com/git/git/commit/b6b3b6a.patch

  2. Patch the original file like this:

    git am /tmp/b6b3b6a.patch
    

Using GitHub diff

  1. Add .diff to a commit URL to get the patch file, example

    github.com/git/git/commit/b6b3b6a.diff

  2. Patch the original file like this:

    git apply -p0 /tmp/b6b3b6a.diff
    

§5.3 Distributed Git - Maintaining a Project




回答5:


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




回答6:


With Git 2.25 (Q1 2020), git format-patch evolves to better use the branch description ("git branch --edit-description") as subject.

See commit bf8e65b, commit a92331d, commit 46273df (15 Oct 2019) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster -- in commit b75ba9b, 10 Nov 2019)

format-patch: teach --cover-from-description option

Signed-off-by: Denton Liu

Before, when format-patch generated a cover letter, only the body would be populated with a branch's description while the subject would be populated with placeholder text.

However, users may want to have the subject of their cover letter automatically populated in the same way.

Teach format-patch to accept the --cover-from-description option and corresponding format.coverFromDescription config, allowing users to populate different parts of the cover letter (including the subject now).

The git config documentation now includes:

format.coverFromDescription:

The default mode for format-patch to determine which parts of the cover letter will be populated using the branch's description.

And git format-patch:

--cover-from-description=<mode>:

Controls which parts of the cover letter will be automatically populated using the branch's description.

  • If <mode> is message or default, the cover letter subject will be populated with placeholder text.
    The body of the cover letter will be populated with the branch's description. This is the default mode when no configuration nor command line option is specified.

  • If <mode> is subject, the first paragraph of the branch description will populate the cover letter subject.
    The remainder of the description will populate the body of the cover letter.

  • If <mode> is auto, if the first paragraph of the branch description is greater than 100 bytes, then the mode will be message, otherwise subject will be used.

  • If <mode> is none, both the cover letter subject and body will be populated with placeholder text.



来源:https://stackoverflow.com/questions/2082296/how-do-patches-work-in-git

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!