本文介紹在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 <[email protected]>
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
應用更新檔
git am /tmp/buding/0001-C.patch
二、鈎子
當版本庫出現送出或更新檔這樣的特殊事件時,會觸發執行一個或多個任意的腳本。
-
前置(pre)鈎子
會在動作完成前調用,要在變更應用前進行準許、拒絕或調整操作,可以使用這種鈎子
-
後置(post)鈎子
在動作完成之後調用,常用來觸發郵件通知或進行額外處理
安裝鈎子
[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
.........
建立一個鈎子
[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