Search notes:

git format-patch

Prepare patches for e-mail submission.

Example

First, we need to create a repository, …
git init -q repo
pushd repo > /dev/null
Github repository about-git, path: /commands/format-patch/init-repo
… add some files to the repo…
cat <<SRC > main.c
#include <stdio.h>
int main() {
   printf("Hello wrold!\n");
   return 0;
}
SRC

cat <<MKF> Makefile
all: prog

main.o: main.c
	@gcc -c main.c -o main.o

prog: main.o
	@gcc main.c -o prog
MKF

cat <<IGN> .gitignore
prog
main.o
IGN
Github repository about-git, path: /commands/format-patch/create-files
… and commit them:
git add .
git commit . -q -m  'Initial commit (main.c Makefile .gitignore)'
Github repository about-git, path: /commands/format-patch/add-commit
Leave the created repo:
popd > /dev/null
Github repository about-git, path: /commands/format-patch/popd
Clone the repository and pushd into the cloned repository's directory:
git clone -q repo repo.clone
pushd repo.clone
Github repository about-git, path: /commands/format-patch/clone
Create a branch for our changes:
git switch -q -c improvements
Github repository about-git, path: /commands/format-patch/create-branch
First, we fix the typo in the source file (and commit it):
sed -i s/wrold/world/ main.c
git commit . -q -m 'fix typo (wrold -> world)'
Github repository about-git, path: /commands/format-patch/fix-typo
Second, we add the -O2 compiler optimization in the Makefile (and commit it also):
sed -i 's/gcc main.c/gcc -O2 main.c/' Makefile
git commit . -q -m 'Optimize with -O2'
Github repository about-git, path: /commands/format-patch/optimize
We're now ready to create the patch files (in a subdirectory named patches):
git format-patch -q master..improvements -o patches
Github repository about-git, path: /commands/format-patch/format-patch
We can now send the files per mail (in this example, we simply cp them):
mv patches ../repo
Github repository about-git, path: /commands/format-patch/send-patches
Get ouf the directory …
popd > /dev/null
Github repository about-git, path: /commands/format-patch/popd
… and back to the original repository's directory to apply the patch files:
pushd repo
git apply patches/*
Github repository about-git, path: /commands/format-patch/apply-patches
git status shows that the patch files were applied to the sources.
The maintainer of the repository might now commit the files to the original repository.

See also

git am
git send-email
git commands

Index