git使用小抄

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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值