Skip to content

--force-if-includes vs --force-with-lease #64

@mauroporras

Description

@mauroporras

Both --force-if-includes and --force-with-lease are flags used with the git push command, but they offer different levels of safety when forcibly pushing changes to a remote repository. Here's a breakdown of their differences:

--force-with-lease:

  • Purpose: Allows force pushing only if the remote branch currently matches what you expect based on your local copy. This prevents overwriting other people's work accidentally.
  • Safety: Safer than --force alone, but still risky. If someone else pushed commits to the remote branch before you, using --force-with-lease will still overwrite their work.
  • Usage: Use cautiously, and ideally after explicitly fetching the remote branch to ensure you have the latest information.

--force-if-includes:

  • Purpose: Adds an extra layer of safety to --force-with-lease. It checks if your local branch includes all the commits present in the remote branch before allowing the push.
  • Safety: More cautious than --force-with-lease, reducing the chance of accidentally overwriting work. However, it can still be bypassed if you haven't fetched the remote branch recently.
  • Usage: Recommended alongside --force-with-lease for an extra safety net, but be aware of the limitations of relying solely on --force-if-includes.

Summary:

  • Use --force-with-lease sparingly and with caution, primarily after fetching the remote branch.
  • Use --force-if-includes along with --force-with-lease for added safety, but remember it's not foolproof.
  • Best practice: Avoid forceful pushes whenever possible. Try alternative workflows like rebasing or merging to integrate your changes.

Additional Resources:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions