Why is git push --force-with-lease the only safe alternative to git push --force after history rewrite?
blind git push --force overwrites the remote branch regardless of whether a teammate has uploaded new concurrent commits at the same moment, permanently wiping its code from the server. While --force-with-lease first checks that the local remote branch pointer (origin/branch) accurately matches the actual state on the server; if someone else uploads a new commit that you haven't seen yet, Git rejects the push and asks you to update first.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
Practical comparison:
#1. Dangerous forced payment (may erase your colleagues' code without your knowledge):
git push --force origin feature/login
#2. Secure Forced Payment with Lease Verification:
git push --force-with-lease origin feature/loginHow does protection work?
- Checks Git: Does your machine know all the commits currently on
origin/feature/login? - If your colleague uploads a new commit: Git rejects the operation with the message:
[remote rejected] feature/login -> feature/login (stale info) - This prevents data loss and forces you to check and review your colleague’s code before re-paying.
- Using git push --force-with-lease immediately after a git fetch command without reviewing what was fetched, because git fetch updates the reference pointer locally and voids lease protection.
- Rely on force push as a routine rather than using separate feature branches and Branch Protection Rules.
- How do you adjust git config to force --force-with-lease by default as a safe alternative?
- What are the procedures in place at platforms like GitHub to completely prevent Force Push operations on sensitive branches?
A teammate accidentally force-pushes a shared branch and hides other commits. How do you recover safely?
Freeze further pushes, identify the last known good commit from local reflogs, pull-request references, CI logs, or another clone, then restore the branch with a reviewed force-with-lease push. Communicate the recovery commit and ask contributors to rebase or reset only after the shared branch is restored.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
One safe recovery pattern is:
# Inspect local history and find the good commit.
git reflog show origin/main
git show <good-commit>
# Create a recovery branch for review, then restore only after agreement.
git switch -c recovery-main <good-commit>
git push --force-with-lease origin recovery-main:mainUse a pull request or a second reviewer when possible. Protect the branch afterward so normal contributors cannot force-push to it.
- Immediately force-pushing your own local branch without confirming it contains every lost commit.
- Using --force instead of --force-with-lease during recovery and overwriting new recovery work.
- Which Git hosting features can prevent force pushes to protected branches?
- How can git reflog help after a commit is no longer reachable from a branch?
What is the difference between git fetch and git pull, and why is it recommended to set pull.ff only in Git settings?
git fetch downloads new objects and references from the remote repository and updates tracking branches (such as origin/main) without merging them or modifying your local files. git pull performs two sequential steps: fetch and then an immediate merge or rebase into your current branch. Set pull.ff only so an update cannot create an unintended merge commit when your local branch diverges from the remote branch; this forces you to choose how to integrate the changes.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
Architectural syntax for git pull:
git pull = git fetch + git merge (or git rebase)Controlling safe behavior globally:
# Prevent unexpected merge commits from being created when fetching:
git config --global pull.ff onlyIf the local branch diverges from the remote branch, Git will reject the pull and display a clear message advising you to either reorder your commits via git pull --rebase or merge them explicitly, keeping the repository history clean.
- Using blind git pull while there are local edits, creating a messy auto-merge with auto-commit headers like Merge branch 'main' of ... distorting the project history.
- Belief that git fetch changes any line in working folder files; git fetch is completely safe and never changes your existing code.
- How do you set the Fast-Forward Only policy globally in Git?
- What's the difference between git pull --rebase and git pull --ff-only?