You meant to push fix/login. The terminal was still on main, and the push triggered a deploy. A local hook can catch that slip. Branch protection on the server makes the same rule apply to the whole team.
Block the destination branch with a pre-push hook
From the repository root, ask Git where it looks for hooks. This respects the hooks directory you already use.
git rev-parse --git-path hooks
Look for pre-push in that directory. If a hook manager owns it, register the check below through the manager. If several repositories share the directory, changes there apply to all of them.
If there is no pre-push hook, create the directory if needed and save this check there as pre-push. Start the file with #!/bin/sh. Replace main and release with the branch names that deploy. From the hooks directory, run chmod +x pre-push.
while read -r src oid target old
do
branch=${target#refs/heads/}
case "$branch" in
main|release)
echo "Blocked $branch" >&2
exit 1
;;
esac
done
If you already have a standalone pre-push hook, keep both checks. In the active hooks directory, first check that deploy-check and pre-push.original are unused. If the existing hook relies on its filename, add the guard through that hook’s own extension mechanism. Otherwise, save the guard as deploy-check, including the #!/bin/sh line. Rename the existing hook to pre-push.original, keeping its executable bit.
Create a new pre-push with #!/bin/sh on the first line and the following body. Git supplies one input line per ref. Saving that list lets each check read it from the beginning, even if the other check has already read it all.
refs=$(mktemp) || exit 1
trap 'rm -f "$refs"' 0
cat >"$refs" || exit 1
dir=$(dirname "${0}") || exit 1
status=0
original="$dir/pre-push.original"
if [ -x "$original" ]; then
"$original" "$@" <"$refs" ||
status=1
fi
"$dir/deploy-check" "$@" <"$refs" ||
status=1
exit "$status"
Run chmod +x pre-push deploy-check in the hooks directory. A disabled original hook stays disabled. The deploy check runs on every push that calls the wrapper. Each enabled check gets Git’s remote name and URL and can stop the entire push.
Test in a disposable repository with a local bare remote. From the repository root, send a feature branch and main in one push. Both updates should stop, with this message for the destination branch.
Blocked main
Then push to a feature branch on its own. It should go through if all enabled checks pass. If your original hook is executable, trigger one of its rejections and check that the remote branch stays put. Each clone needs the hook configured, and git push --no-verify skips it. Use the server rule to enforce the policy across clones and tools.
Require a pull or merge request on your Git host
Apply the rule to every branch that deploys. Block direct and force pushes (on feature branches, use a lease), and let the people who should merge do so through a pull or merge request.
On GitLab, set Allowed to push and merge to No one. Set Allowed to merge for the appropriate roles and keep force pushes disabled.
Check which rules match each branch and review any exceptions for admins, apps, or automation. Test with the access you normally use in a disposable repository before applying the rule to a branch that deploys.
If a direct push has already been rejected, push your commits from a feature branch.
Check the branch and remote before pushing
git branch --show-current
git remote -v
Read the branch name and the remote’s push URL. If the branch command prints nothing, you’re on a detached HEAD. Switch to the branch you intend to push before continuing.
When you are on fix/login and want that name on origin, spell out the destination.
git push origin \
HEAD:refs/heads/fix/login
If your release only needs a tag, preview and push that tag by name.
Review what is waiting to go out
Plain Git can list the commits and files a push would send: see how to preview what you are about to push. If you switch between repositories on a Mac, push& (Pushand) shows the commits waiting to be pushed by branch so you can review them before sending them. See how it works.
If the change has already landed on a shared branch, revert the pushed commit through your team’s usual review.