I'm evaluating someone else's git repository and I don't want to make accidental committed changes.
Is there a way to protect a branch locally just by using 'plain Git'? For example by preventing commit to particular branch?
I only need this to affect my local clone.
No Github or other git hosting service features should be involved. I want this to be as portab as possible.
You could use a git hook.
Place the following script into .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
On Linux, don't forget to chmod +x
the script file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With