有没有办法在本地保护一个git分支?

6za6bjd0  于 2023-04-10  发布在  Git
关注(0)|答案(2)|浏览(134)

我正在评估其他人的git仓库,我不想意外地提交修改。
有没有一种方法可以在本地保护一个分支,只需要使用'plain Git'?例如,通过阻止提交到特定的分支?
我只需要这个来影响我的本地克隆。
不应该涉及Github或其他Git托管服务功能。我希望这是尽可能portab。

3ks5zfa0

3ks5zfa01#

如果它是你的本地克隆,那就是你的仓库。你已经从他们的仓库中获取了历史到你的仓库中,并且正在评估获取的历史。
没有必要保护任何东西,只要不推就行了。似乎可以肯定地说,它们不会无意中抓取。如果你想防止反射性推,你可以

git config remote.origin.pushurl "You really didn't want to do that."

你所做的任何事情影响他们的存储库的唯一方式是你推送或他们获取(新的或更改的)引用和任何新的历史。

niknxzdl

niknxzdl2#

您可以使用git hook。将以下脚本放入.git/hooks/pre-commit

#!/bin/bash

current_branch="$(git branch --show-current)"
for protected_branch in "main" "other_branch_you_want_protected"; do
    if [[ "$protected_branch" == "$current_branch" ]]; then
        echo "ERROR: local branch $current_branch is protected" 
        exit 1
    fi
done

exit 0

在Linux上,不要忘记chmod +x脚本文件。

相关问题