Example
git init -q repo
pushd repo > /dev/null
… 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
git add .
git commit . -q -m 'Initial commit (main.c Makefile .gitignore)'
Leave the created repo:
popd > /dev/null
Clone the repository and pushd into the cloned repository's directory:
git clone -q repo repo.clone
pushd repo.clone
Create a
branch for our changes:
git switch -q -c improvements
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)'
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'
We're now ready to create the patch files (in a subdirectory named patches
):
git format-patch -q master..improvements -o patches
We can now send the files per mail (in this example, we simply
cp
them):
mv patches ../repo
Get ouf the directory …
popd > /dev/null
… and back to the original repository's directory to
apply the patch files:
pushd repo
git 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.