ThisCalculator

Everyday

How to Deny Git Push Force in Claude Code

Updated · Standard formula, shown below

How to Deny Git Push Force in Claude Code

This guide shows you how to stop Claude Code from force-pushing to your Git repositories. Force-pushing can overwrite remote history and cause data loss, so denying it adds a safety net. You’ll need a project with Claude Code installed and a basic understanding of Git commands.

What You’ll Need

  • A terminal or command prompt with Claude Code installed (via npm or native installer).
  • A Git repository where you want to enforce the rule.
  • Basic familiarity with editing files in the repository.

Step-by-Step

  1. Open your project directory in the terminal where you normally run Claude Code.
  2. Create or edit the CLAUDE.md file at the project root. If it doesn’t exist, create it with: touch CLAUDE.md (or use your editor).
  3. Add a clear instruction to CLAUDE.md. Write something like: Never use git push --force or git push -f. Always push normally. Place it near the top so Claude sees it early.
  4. Save the file and close it.
  5. Start Claude Code in the same directory by running claude in the terminal.
  6. Test the rule by asking Claude to force-push: e.g., “Force push to origin main”. Claude should refuse or ask for confirmation, citing the instruction.
  7. If Claude still attempts a force push, add a stronger directive: Under no circumstances may you use the --force flag with git push. This is a hard rule.
  8. For extra protection, consider using a Git hook (see Tips) to block force pushes at the Git level, independent of Claude.

Common Mistakes

  • Writing the rule in a file that Claude doesn’t read. Claude reads CLAUDE.md from the project root and also from your home directory (~/.claude/CLAUDE.md). Make sure you edit the right one.
  • Using ambiguous language like “avoid force push” – Claude might interpret that as optional. Use absolute terms like “never” or “do not”.
  • Forgetting to restart Claude Code after editing CLAUDE.md. Claude reads the file at startup, so you need to restart the session for changes to take effect.
  • Assuming the rule applies to all projects. CLAUDE.md in one project doesn’t affect another.

Tips

  • Add the rule to your global CLAUDE.md (~/.claude/CLAUDE.md) to apply it across all projects.
  • Use a Git pre-push hook to block force pushes entirely. Create .git/hooks/pre-push with a script that checks for the –force flag and exits with an error.
  • If you’re using Claude Code in a team, commit the CLAUDE.md file to the repository so everyone gets the rule.
  • You can also set a Git alias to make force push require confirmation, but that’s not Claude-specific.

Troubleshooting

  • Claude still force-pushes despite the rule. Check that the CLAUDE.md file is in the correct location and that you restarted Claude. If it persists, rephrase the rule more forcefully and test again.
  • Claude doesn’t see the CLAUDE.md file. Run claude from the project root. You can verify by asking Claude “What are my rules?” and it should list the instruction.
  • Force push happens anyway because of a typo. Claude might interpret “git push –force-with-lease” differently. Add a rule that also bans that variant.
  • You want to allow force push in some cases. Write a conditional rule, e.g., “You may force push only to a branch named ‘experimental’.”
  • Git hook not working. Ensure the hook file is executable (chmod +x) and that the script is correct for your shell.

Frequently Asked Questions

Does Claude Code read CLAUDE.md automatically?

Yes, Claude Code reads CLAUDE.md from the project root and from your home directory at startup. It uses these instructions to guide its behavior.

Can I set this rule globally for all my projects?

Yes, edit ~/.claude/CLAUDE.md in your home directory. That applies to every project you open with Claude Code.

What is the difference between –force and –force-with-lease?

–force overwrites the remote branch unconditionally, while –force-with-lease only overwrites if the remote hasn’t changed since you last fetched. The latter is safer, but both can rewrite history. You may want to ban both.

Will this prevent me from force pushing manually?

No, the rule only affects Claude Code. If you run git push –force yourself in the terminal, it will still execute (unless you also set a Git hook).

Can I use a Git hook instead of CLAUDE.md?

Yes, a pre-push hook is a more robust solution because it blocks any force push, not just ones initiated by Claude. You can combine both for defense in depth.

What if Claude ignores the rule and force pushes anyway?

Immediately check your remote history. If you have access, you may be able to restore the previous state using reflog on the remote (if enabled). To prevent future occurrences, add a Git hook and rephrase the rule more strictly.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.