In daily development, code generation and patch application are often things that need to be done. This article records some common usages.
Generate patch
Use patch command
patch all uncommitted changes
git diff > test.patch
Only patch Test.java file git diff Test.java > test.patch
Difference between branches generates patch files
git diff branchname --cached > test.patch
quickly generate patch files according to the directory tree based on submission
git diff commit-id-new commit-id-old --name-only|xargs tar cjvf test.patch.tar.bz2
The latest patch submitted to the node
git format-patch HEAD^
patch between two nodes
git format-patch nodeA nodeB
Export all modified files into patches
git diff > test.patch
Use format_patch command
Generate patches for the last 2 commits
git format-patch HEAD^^
Generate patches for the latest 4 commits git format-patch HEAD^^^^
Generate a modified patch between two commits
git format-patch <r1>..<r2>
Generate a patch for a single commit
git format-patch -1 <r1>
Generate modified patches since a certain commit (excluding this commit)
git format-patch <r1>
Generate all patches submitted from root to r1
git format-patch --root <r1>
Apply patch
git apply
Check patch status
git apply --stat test.patch
Check whether the patch can be applied. If there is no output, it means there is no conflict and you can apply it
git apply --check test.patch
One of the patching scenarios uses the patch generated by git diff
git apply xxx.patch
git am
Another patching command, its difference from git apply
is: git apply
does not apply commit messages, etc. After patching, you need to re-git add and git commit. And git am
can directly apply all the information of the patch, and there is no need to re-git add and git commit.
Apply the patch named test.patch
git am test.patch
When git am fails, it is used to discard the patches that have been added during the am process
git am --abort
When git am fails and the conflict is resolved, this command will then apply the patch
git am --resolved