Sub topic 11
Team practices: aliases, branch scope, and protected branches
This chapter assumes you can rebase and amend commits, you know what origin is, and you have used --force-with-lease and read a stale info rejection. Here the focus moves from one push to team habits: which command people type, which branches they may rewrite, and what the server enforces.
Make the safe command the easy one
Plain --force is short to type, so people reach for it. If the safe form is longer, the unsafe form wins. An alias removes that difference.
git config --global alias.pushf "push --force-with-lease --force-if-includes"
git pushf origin feature/loginThe alias stores the full safe command under one short name. Running git pushf is now faster than typing git push --force. A team can agree on one alias name and share it in onboarding notes.
An alias does not stop anyone from typing git push --force. It only changes which path takes the least effort.
Some options need care in an alias:
--force-if-includesonly has an effect together with--force-with-lease. The alias above always sets both.- Do not put a branch name or a pinned commit in an alias. A fixed expected value would go stale and would reject every later push.
- Always pass the remote and branch yourself, as in
git pushf origin feature/login. Without them, Git uses your push configuration, and the alias may push more than you intended.
Limit which branches may be rewritten
Force pushing is only acceptable where you know who else uses the branch. A simple rule set works well for most teams:
| Branch type | Rewriting allowed? | Reason |
|---|---|---|
| Your own feature branch | Yes, with the safe alias | You are the only author, or you have agreed with the others |
| A branch shared by several people | Only after agreeing with them | A rewrite makes their local copies diverge |
| `main`, `release/*` | No | Others build and deploy from these. Rewrites break their history |
The lease protects you from overwriting a commit you have not seen. It does not tell you whether a teammate has already based work on your commits. If someone has pulled your branch and you rewrite it, their next pull produces conflicts or duplicate commits even though your push was "safe". Tell the team before you rewrite a shared branch.
A name convention helps. For example, a prefix such as feature/ or your username can signal "one owner, rewrite freely", while other branches are treated as shared.
Enforce the rules on the server
Habits fail under pressure, so back them with server settings. Hosting services such as GitHub and GitLab offer protected branches. A protected branch can block force pushes and deletion for everyone, including administrators if you choose.
The three layers cover different failures:
- The alias makes the safe command the default.
- The lease check runs on your machine and catches commits you have not seen.
- The protected branch rule runs on the server and catches mistakes on branches that should never be rewritten, such as pushing
--forcetomainby accident.
Protect the long-lived branches (main, release branches) and leave short-lived feature branches unprotected so that people can rebase them. Use pull requests for changes to protected branches, and clean up with a merge or squash instead of rewriting shared history.
A short checklist before you force push
- Confirm the branch is yours, or that everyone using it has agreed.
- Run
git fetch originandgit log --oneline HEAD..origin/feature/loginto see anything new. - Integrate what you find, then push with
git pushf origin feature/login. - If the push is rejected with
stale info, do not retry with--force. Go back to step 2.
Exercise
Your team has main protected against force pushes. A colleague says this makes --force-with-lease unnecessary on feature branches. Is that right?
Show answer
No. Protection on main says nothing about feature branches, which stay unprotected so people can rebase them. Force pushing to a shared feature branch can still overwrite a teammate's commits, so the lease check (via an alias such as git pushf) is still needed there.
Exercise
Why should a team alias not include a pinned value such as --force-with-lease=feature/login:abc123?
Show answer
The pinned commit would become outdated as soon as the remote branch moves, so the alias would reject later pushes (or be edited to bypass the check). The expected value belongs in a one-off command, not in a shared alias.