The problem is that git push assumes that the remote branch can be fast-forwarded to your local branch. That is, that the only difference between the local and remote branches is that the local one has some new commits at the end, like:

Z--X--R         <- origin/some-branch (can be fast-forwarded to Y commit)
       \        
        T--Y    <- some-branch

When you perform git rebase, commits D and E are applied to the new base and new commits are created. This means that, after rebase, you have something like:

A--B--C------F--G--D'--E'   <- feature-branch
       \  
        D--E                <- origin/feature-branch

In this situation, the remote branch can't be fast-forwarded to local. Although, theoretically, the local branch can be merged into remote (as the current state of remote is no longer needed), git push only performs fast-forward merges and therefore it throws an error.

The --force option ignores the state of the remote branch and sets it to the commit you're pushing into it. So git push --force origin feature-branch simply overrides origin/feature-branch with the local feature-branch.

In my opinion, rebasing feature branches on master and force-pushing them back to remote repository is OK as long as you're the only one who works on that branch.

Answer from KL-7 on Stack Overflow
Top answer
1 of 14
1053

The problem is that git push assumes that the remote branch can be fast-forwarded to your local branch. That is, that the only difference between the local and remote branches is that the local one has some new commits at the end, like:

Z--X--R         <- origin/some-branch (can be fast-forwarded to Y commit)
       \        
        T--Y    <- some-branch

When you perform git rebase, commits D and E are applied to the new base and new commits are created. This means that, after rebase, you have something like:

A--B--C------F--G--D'--E'   <- feature-branch
       \  
        D--E                <- origin/feature-branch

In this situation, the remote branch can't be fast-forwarded to local. Although, theoretically, the local branch can be merged into remote (as the current state of remote is no longer needed), git push only performs fast-forward merges and therefore it throws an error.

The --force option ignores the state of the remote branch and sets it to the commit you're pushing into it. So git push --force origin feature-branch simply overrides origin/feature-branch with the local feature-branch.

In my opinion, rebasing feature branches on master and force-pushing them back to remote repository is OK as long as you're the only one who works on that branch.

2 of 14
1005

Instead of using -f or --force developers should use

--force-with-lease

Why? Because it checks the remote branch for changes which is absolutely a good idea. Let's imagine that James and Lisa are working on the same feature branch and Lisa has pushed a commit. James now rebases his local branch and is rejected when trying to push. Of course James thinks this is due to rebase and uses --force and would rewrite all Lisa's changes. If James had used --force-with-lease he would have received a warning that there are commits done by someone else. I don't see why anyone would use --force instead of --force-with-lease when pushing after a rebase.

🌐
Graphite
graphite.com › guides › git-rebase-force
Force pushing after a Git rebase
After resolving any conflicts, use: ... Repeat this until all conflicts are resolved and the rebase completes. For a more detailed walkthrough see this guide on resolving conflicts in Git. Force push to remote: After successfully rebasing locally, you'll need to force push to update the remote repository:
Discussions

Git - After Rebase, recommend Push (Force)
After rebasing a branch onto its parent, Git status reports that the local and remote branches have "diverged" and recommends pulling. This is inappropriate advice at this point in the "rebase onto" workflow. If followed, it can be quite... More on github.com
🌐 github.com
4
September 14, 2023
git - rebase your own branch and force push - Software Engineering Stack Exchange
Is it ok when I do rebase and force push in branch which is used only by me? And will I have a problem after merge this branch in master later? ... git push origin new_feature -f -- I'm able to push it only with force because the history is different in current and remote feature branch. More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
September 13, 2017
Force push and rebase - LLVM Project - LLVM Discussion Forums
LLVM GitHub User Guide — LLVM 18.0.0git documentation says “In general, you should avoid rebasing a Pull Request and force pushing to the branch that’s the root of the Pull Request during the review.” I very much understand that The fidelity of preserving inline comments after a force ... More on discourse.llvm.org
🌐 discourse.llvm.org
6
September 29, 2023
git - Why do I need a force push after a rebase? - Stack Overflow
I read a lot of articles about force push after a rebase but I still don't understand everything, I though. What it's not clear to me is in which way a rebase is rewriting git history. Suppose to b... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/git › should i be pushing after rebase?
r/git on Reddit: Should I be Pushing after Rebase?
April 9, 2018 -

Hi everyone!

I'm wondering what is the best practice when working with squash+rebase. Consider the following example:

I have a master branch, and I create a local feature branch out of it. I'm working on the local feature branch, commiting often, and now it's time to squash and rebase. I did that on my local branch, and now I'm able to fast-forward the changes into my local master. Now that everything local is sorted out, I can push my master into origin.

Question is - what do I do with my local feature branch. The origin history is different from my local feature branch. Should I force push it? I kinda have to if I want to create a Github code-review. What happened after I fixed couple of code-review issues, I need to squash+rebase again and AGAIN force push my local feature branch?

🌐
Substack
webdeveloperdiary.substack.com › diary of a web developer › using git to successfully push a modified or rebased branch
Using Git to Successfully Push a Modified or Rebased Branch
April 3, 2023 - As it’s usually not recommended to rebase a shared branch, these two conditions will typically be true. However, if we want some safeguards, we can use: ... --force-with-lease alone, without specifying the details, will protect all remote refs that are going to be updated by requiring their current value to be the same as the remote-tracking branch we have for them. In other words, this command will prevent data loss by failing if: Any of our teammates have pushed additional commits to the branch, or
🌐
GitHub
github.com › microsoft › vscode › issues › 193143
Git - After Rebase, recommend Push (Force) · Issue #193143 · microsoft/vscode
September 14, 2023 - The only correct action after an intentional and successful "rebase onto" is a Push (Force).
Author   microsoft
🌐
Adam Johnson
adamj.eu › tech › 2023 › 10 › 31 › git-force-push-safely
Git: Force push safely with --force-with-lease and --force-if-includes - Adam Johnson
$ git push -f ... To github.com:adamchainz/example.git + 1fd4fb8...e025fa5 camembert -> camembert (forced update) A force push bypasses the fast-forward protection and always updates the remote branch, no questions asked.
Find elsewhere
🌐
LLVM Discussion Forums
discourse.llvm.org › llvm project
Force push and rebase - LLVM Project - LLVM Discussion Forums
September 29, 2023 - LLVM GitHub User Guide — LLVM 18.0.0git documentation says “In general, you should avoid rebasing a Pull Request and force pushing to the branch that’s the root of the Pull Request during the review.” I very much understand that The fidelity of preserving inline comments after a force ...
🌐
GitLab
docs.gitlab.com › topics › git › git_rebase
Rebase and resolve merge conflicts | GitLab Docs
After you run git rebase --continue, you cannot abort the rebase. ... Complex Git operations like squashing commits, resetting a branch, or rebasing rewrite branch history. Git requires a forced update for these changes. Force pushing is not recommended on shared branches, because you risk ...
🌐
Medium
medium.com › towardsdev › understanding-the-power-of-git-push-force-with-lease-30f73858e0dc
Understanding The Power of ‘git push — force-with-lease’ | by Daniel Ben Hayoun | Towards Dev
April 5, 2024 - The commits C and D are now based on the latest commit G from the main branch, thus updated to C’ and D’. They may have been amended or squashed during the rebase, so they’re no longer the same commits as before, although they represent the same changes to the code. Join Medium for free to get updates from this writer. ... Now, if you try to push to the remote ‘cool-feature’, Git will refuse to do so because the histories don’t match. In the remote repository it’s still “C and D,” but in your local, it has become “C’ and D’”. This is why we need the --force-with-lease.
Top answer
1 of 3
16

e in your second diagram isn't the same commit as e in your first diagram. It may make the same change (although it doesn't necessarily), and it may have the same message (although it doesn't necessarily), but its ancestor and the contents of the tree are both different, so it's a different commit. Likewise your second f can't possibly be the same commit as your first f, and the same applies to g and h. Normally people explaining rebasing would denote the yellow commits in your second diagram as e', f', g', and h' to show the difference.

When you go to push, the upstream has h and you're pushing h'. h' isn't the same commit as h, and it also isn't a descendant of h (we can't reach h' just by tacking some more commits onto h), so this is called a "non-fast-forward" push.

In the time while h was published as feat1, someone else could have made some more commits i and j, beginning with h. After you push h', they're left with commits that are still descendants of h, which is no longer a part of feat1 (and probably no longer a part of any branch). It's not actually technically hard for them to fix that situation (it's just another rebase, with --onto) but it's an annoyance and it forces people to stop and figure out what's going on, so git puts a roadblock in your way to make sure it's something that you really intended to do.

2 of 3
3

If you pushed your changes upstream to your feature branch before the rebase, then your local copy will be in a different order to the upstream - hence needing a force push.

However, if you merge your now rebased local copy onto the main branch, it will be a fast forward only merge so can be pushed upstream to main without needing a force.

🌐
Kapstan
kapstan.io › blog › mastering-git-rebase-a-comprehensive-guide
Mastering Git Rebase: A Comprehensive Guide | Kapstan
August 14, 2024 - After the rebase is complete, your ... · If you've already pushed this branch before, you'll need to force push:git push origin feature --forceRemember, always use force push with caution, especially on shared branc...
🌐
Reddit
reddit.com › r/git › [deleted by user]
[deleted by user] : r/git
June 28, 2017 - When you force push, it's almost always better to use git push --force-with-lease (instead of git push --force or git push -f). This removes some of the risk involved because if you pull, rebase, and then someone else pushes a new commit, --force-with-lease will fail instead of overwriting ...
🌐
Medium
noaabarki.medium.com › dont-underestimate-the-push-force-5cba944a246d
Don’t Underestimate the push — force | by Noaa Barki | Medium
January 24, 2020 - Your only option is to fight fire with fire and push — force this commit back to the branch on top of the bad one: ... Imagine working on a feature branch, you pulled some changes, created a few commits and completed your part of the feature ...
🌐
University of Toronto
microfluidics.utoronto.ca › help › help
Git rebase · Git · Topics · Help · GitLab
You must have permissions to force push to branches. To use Git to rebase your branch against the target branch: Open a terminal and change to your project directory. Ensure you have the latest contents of the target branch. In this example, the target branch is main: ... Optional. Create a backup of your branch: ... Changes added to my-branch after this point are lost if you restore from the backup branch.
🌐
GitHub
gist.github.com › hugoduraes › ed79ac69866682867d74ecc62ba4cdbb
git-pull-after-forced-push.md · GitHub
If the commits you removed (with git push -f) have already been pulled into the local history, they will be listed as commits that will be reapplied - they would need to be deleted as part of the rebase or they will simply be re-included into ...
🌐
David Walsh
davidwalsh.name › git-force-push
git Force Push
August 19, 2022 - Integrate the remote changes (e.g. ... 'git push --help' for details. Commonly developers will use the --force or -f flags during a push to force pushing code changes:...
🌐
ITNEXT
itnext.io › git-force-vs-force-with-lease-9d0e753e8c41
Git Force vs Force with Lease. And When to Use Them | by Mohammad-Ali A'RÂBI | ITNEXT
April 7, 2022 - Update: When doing a force push on the IntelliJ IDE (since version 2021.3), it actually does a force with lease. When pushing to the remote repository, git will reject the push if the remote one is not an ancestor of the local one. That is called the “fast-forward rule”. Git does that because it suspects that someone has pushed some new commits to the remote branch you’re trying to push into.