git pre-push hook
Git PUSH hook Pre
2023-09-27 14:24:37 时间
This article introduces git pre-push hook.
Problem
In Lean project, we use a modified version of Google’s C++ style checker. I want to automatically run the checker over the changed files before I push commits to git repositories. This is useful because it prevents me from accidentally pushing the commits which contain style problems.
Solution: git pre-push hook
Since git 1.8.2, git introduced pre-push hook which is executed before actual push operation is performed. The following two steps solve the problem.
- Create
<PROJECT_ROOT>/.git/hooks/pre-push
file with the following contents:
#!/usr/bin/env bash IFS=' ' DIR="$( cd "$( dirname "$0" )" && pwd )" CHECKER=$DIR/../../src/cmake/Modules/cpplint.py while read local_ref local_sha remote_ref remote_sha; do CHANGED_FILES=`git diff --name-only $local_sha $remote_sha | grep '\(cpp\|h\)$'` if [ ! -z "$CHANGED_FILES" -a "$CHANGED_FILES" != " " ]; then echo $CHANGED_FILES | xargs $CHECKER RET=$? if [ $RET -ne 0 ]; then echo "There is error(s) from style-check. Please fix them before push to the repo." exit $RET fi fi done exit 0
- Give “exec” permission to the file
chmod +x <PROJECT_ROOT>/.git/hooks/pre-push
Note that you need to change CHECKER
variable if you want to use other checkers.
相关文章
- IntelliJ IDEA 创建 Git 分支并且 Push 到远程
- git分布式版本控制(十)
- Git 的origin和master分析 ***
- Git 命令 clone、add、commit、push 介绍 与 项目实战
- git push上传代码到gitlab上,报错401/403(或需要输入用户名和密码)
- Git 使用总结
- 简单的处理git add ,git commit,git push 脚本
- 使用SSH方式实现Git远程连接GitHub/gitlab
- git bash字体设置
- Git不要只会pull和push,试试这5条提高效率的命令
- Git复习(五)之多人协作、git push失败、git pull失败
- git实现服务器自动push拉取代码--webhooks
- git第一次以及第n次提交代码至远程仓库步骤
- git pull与git fetch及git merge与git rebase的区别
- git学习--更新本地仓库单独文件
- 【Git】Git 标签使用 ( 创建并查询标签 | 推送单个标签到远程仓库 | 推送所有标签到远程仓库 | 删除远程仓库的标签 )
- at_today git--一文弄懂git的工作区、索引区、本地仓库、远程仓库以及add、commit、push三个操作
- 一张脑图带你掌握Git命令
- Pipeline load and load from git
- 解决Windows Git Bash中文乱码问题
- 在本地设置一个或者多个id_rsa 来连接 github 仓库并实现 git push / git pull
- git push 时发生 error: failed to push some refs to 错误 (解决办法)
- 如何使用Git 优雅的版本回退呢?