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

应用补丁

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
posted @ 2018-02-28 13:34  BXBZ—边学边做  阅读(344)  评论(0编辑  收藏  举报