首页 > 解决方案 > 是否有一段代码可以防止更改合并/推送到 git?

问题描述

如果我需要一些额外的代码来在我的本地服务器上运行某些东西,但需要记住在推送到 git 或合并到受保护的分支之前将其删除。

如果我忘记删除该代码,是否有一段代码(或 vscode 扩展)可以通知 git 或在推送时通知我。喜欢//TODO:或git可以识别的东西?

标签: gitvisual-studio-code

解决方案


在 git 方面:您可以在本地 repo 中添加一个 pre-push 钩子来扫描代码中的禁用词,如果找到该词则取消推送。

例如,这是一些寻找的代码dontpush(它使用git grep -i,任何大小写"DoNtPuSh"都可以)并且如果在您的本地提交中找到它就不会推送:

# file '.git/hooks/pre-push' :
#!/bin/bash

remote="$1"
url="$2"

z40=0000000000000000000000000000000000000000

while read local_ref local_sha remote_ref remote_sha
do
    if [ "$local_sha" = $z40 ]
    then
        # Delete: nothing to do on deleted branches
    else
        # Check if "dontpush" appears in the local commit :
        dontpush=`git grep -n -i dontpush $local_sha -- $files`
        if [ -n "$dontpush" ]
        then
            # print 'git grep' output on STDERR,
            # remove the leading "sha:" on each line
            (echo $dontpush | sed -e 's/^[^:]*://') | cat >&2
            echo >&2 "*** Found 'dontpush' tag in $local_ref, not pushing"
            exit 1
        fi
    fi
done

exit 0

推荐阅读