TortoiseGit Push to Public Remote Without Confirmation Prompt
0 reputation · 09 Feb 2023, 23:52 UTC
0 reputation · 09 Feb 2023, 23:52 UTC
The goal is to prevent TortoiseGit from pushing local commits to a publicly accessible remote without an explicit warning or confirmation. The Push dialog sends commits to the remote URL stored in .git/config, and TortoiseGit does not classify remotes as public or private.
No TortoiseGit setting appears to warn or block before pushing to a new remote that may expose repository contents publicly. Access control depends on the remote host and supplied credentials, and behavior may differ between TortoiseGit versions and between PuTTY-based and OpenSSH-based SSH configurations.
Is there a TortoiseGit option to require confirmation before pushing to a remote that may be publicly accessible? If no such option exists, can a client-side hook enforce this boundary, and does enforcement remain consistent across recent TortoiseGit releases?
34775 reputation · 10 Feb 2023, 04:54 UTC
There is no built‑in TortoiseGit setting that classifies a remote as "public" or that automatically prompts the user before pushing to a potentially public URL. The Push dialog simply forwards the command to the underlying Git executable, which follows the standard Git configuration without any visibility checks.
pre‑push HookThe most reliable way to guard against accidental pushes to a public destination is to install a pre‑push hook in the repository’s .git/hooks directory. Git will invoke this script before any push is sent, and if the script exits with a non‑zero status the push is aborted. TortoiseGit respects this exit code and displays the hook’s error message in the Push dialog.
# .git/hooks/pre-push
#!/usr/bin/env bash
# Abort pushes to any remote whose URL contains "github.com" or "gitlab.com"
# Adjust the list below to match your public hosts.
PUBLIC_HOSTS=("github.com" "gitlab.com" "bitbucket.org")
# Extract the remote name and URL
REMOTE_NAME=$1
REMOTE_URL=$(git remote get-url $REMOTE_NAME)
for host in "${PUBLIC_HOSTS[@]}"; do
if [[ "$REMOTE_URL" == *"$host"* ]]; then
echo "\n*** ERROR: Pushing to public remote '$REMOTE_URL' is blocked by the pre-push hook. ***\n" >&2
exit 1
fi
done
exit 0
Make the script executable:
chmod +x .git/hooks/pre-push
When you attempt to push, TortoiseGit will now show the hook’s error message and refuse to send the commit. The hook runs in the same environment as the Git command line, so it works across all recent TortoiseGit releases (from 2.x onward).
.git/hooks and is not version‑controlled; each clone must install it manually or use a post‑clone script.--no-verify, but this flag is rarely used in the TortoiseGit GUI.pre-push script with the list.To fine‑tune the hook, it would help to know whether you use SSH or HTTPS URLs for your remotes and whether any custom domain names (e.g., a corporate GitLab instance) should be exempt. If you can share the pattern of URLs you consider “public”, the hook can be adjusted accordingly.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.