生成補丁git
[root@localhost buding]# echo B > file;git add file;git commit -m "B" [master a8a93f8] B 1 files changed, 1 insertions(+), 1 deletions(-) [root@localhost buding]# echo C >> file;git add file;git commit -m "C" [master 9eae16f] C 1 files changed, 1 insertions(+), 0 deletions(-) [root@localhost buding]# echo D >> file;git add file;git commit -m "D" [master e2f238b] D 1 files changed, 1 insertions(+), 0 deletions(-) [root@localhost buding]# cat file B C D [root@localhost buding]# git show-branch --more=5 master [master] D [master^] C [master~2] B [master~3] A [root@localhost buding]# git format-patch -1 0001-D.patch [root@localhost buding]# git format-patch -2 0001-C.patch 0002-D.patch [root@localhost buding]# git format-patch -3 0001-B.patch 0002-C.patch 0003-D.patch [root@localhost buding]# git format-patch master~2..master 0001-C.patch 0002-D.patch [root@localhost buding]# cat 0001-B.patch From a8a93f836eacad245b518a3c92f2e17c2fc984a6 Mon Sep 17 00:00:00 2001 From: tong <tong@test.com> Date: Wed, 28 Feb 2018 12:00:06 +0800 Subject: [PATCH 1/3] B --- file | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/file b/file index f70f10e..223b783 100644 --- a/file +++ b/file @@ -1 +1 @@ -A +B -- 1.7.1
應用補丁vim
git am /tmp/buding/0001-C.patch
當版本庫出現提交或補丁這樣的特殊事件時,會觸發執行一個或多個任意的腳本。post
安裝鉤子code
[root@localhost buding]# cat .git/hooks/pre-commit.sample #!/bin/sh ...... if git rev-parse --verify HEAD >/dev/null 2>&1 then against=HEAD else # Initial commit: diff against an empty tree object against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 fi .........
建立一個鉤子orm
[root@localhost tmp]# mkdir hooktest [root@localhost tmp]# cd hooktest/ [root@localhost hooktest]# git init Initialized empty Git repository in /tmp/hooktest/.git/ [root@localhost hooktest]# touch a b c [root@localhost hooktest]# git add a b c [root@localhost hooktest]# git commit -m "added a, b, and c" [master (root-commit) c9ecaec] added a, b, and c 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 a create mode 100644 b create mode 100644 c 建立一個鉤子,用來阻止包含「broken」這個詞的變動被檢入 vim pre-commit echo "Hello, I'm a pre-commit script!" >&2 if git diff --cached | grep '^\+' | grep -q 'broken';then echo "ERROR:Can't commit the word 'broken'" >&2 exit 1 # reject fi exit 0 # accept