Push to remote branch failing after squashing local commits

I made three commits to a branch and pushed each of them to remote. I then needed to squash the commits, so I used:

git reset --soft HEAD~3 && git commit
git push --force

At the second line, I got a fatal error:

The current branch [BRANCH-NAME] has no upstream branch.
To push the current branch and set the remote as upstream.

I tried its suggested command with git push --set-upstream origin [BRANCH-NAME], but am being told that the tip of the current branch is behind its remote counterpart. Looking back, it makes sense, as I’m currently on index 1 of my local branch while the head is at index 3 on the remote.

What I want to do now is basically to have the squashed version of the branch (which I have locally) replace the non-squashed version that is in the remote. What’s the right approach here?

I’ve looked at this and a couple of others but they haven’t helped.

  • Tortoise SVN : How to ignore bin contents from commit
  • How to create new branch in a remote?
  • Git stash then checkout each time
  • Remove a specific commit but keep all the commits afterward
  • egit not cloning submodules Recursively
  • Discard all changes in Xcode 4.2
  • Keeping page changes history. A bit like SO does for revisions
  • GitHub: Fork is always one commit behid afer pull accepted
  • 2 Solutions collect form web for “Push to remote branch failing after squashing local commits”

    You need to add --force onto that suggested command:

    git push --set-upstream --force origin [BRANCH-NAME]

    After you’ve run it with --set-upstream, you’ll be able to just type git push (or git push -f where necessary) from then on.

    You need to rewrite the git history as these changes have been committed to remote. In general, avoid rewriting history unless you absolutely have to. Having trivial commits is not reason to squash pushed commits. If they can stay, let them stay.

    For the git remote repo, if you do wish to proceed – I assume you know to squash the commits on your local repo ( git rebase -i is straightforward). After the squash, push with a -f – a force push.

    Git Baby is a git and github fan, let's start git clone.