commit hook
在.git/hook/pre-commit填写如下内容:
#!/bin/sh
#
# To enable this hook, rename this file to "pre-commit".
SCRIPT_DIR=$(dirname "$0")
# script_dir: git/hook/
${SCRIPT_DIR}/../../build/test_func
if [ $? -eq 0 ]; then
echo "OK"
else
echo "test fail"
exit 1
fi
# Redirect output to stderr.
exec 1>&2
在提交commit时会自动运行build/test_func程序, 如果程序运行失败, 则打印test fail.
push hook
类似的, 我们可以在push前触发测试, 修改.git/hook/pre-commit内容:
#!/bin/sh
# An example hook script to verify what is about to be pushed. Called by "git
# push" after it has checked the remote status, but before anything has been
# pushed. If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
# <local ref> <local sha1> <remote ref> <remote sha1>
#
# This sample shows how to prevent push of commits where the log message starts
# with "WIP" (work in progress).
remote="$1"
url="$2"
SCRIPT_DIR=$(dirname "$0")
# script_dir: git/hook/
# run test case:${project_root}/build/test_func
${SCRIPT_DIR}/../../build/test_func
if [ $? -eq 0 ]; then
echo "pre-push OK"
else
echo "test fail"
exit 1
fi
# Redirect output to stderr.
exec 1>&2
那么在每次push前都会触发测试, 如果测试错误, 则停止push.
Support for password authentication was removed on August 13, 2021 错误处理.
在push代码时出现下列错误:
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
解决方法:
github页面:My Account → Settings → Developer settings → Personal access tokens → Generate new token.
复制新token后重新设置remote:
git remote set-url origin https://<token>@github.com/<username>/<repo>
删除未跟踪文件
git clean -fd
cherry-pick
cherry-pick用来将其他分支的commit复制到本分支
# 将master 分支的commit的修改复制到本分支
git checkout dev
git cherry-pick master afe3