5

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.

Community
  • 1
  • 1
NeonBlueHair
  • 1,139
  • 2
  • 9
  • 22

2 Answers2

7

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.

jonnystoten
  • 7,055
  • 2
  • 28
  • 36
  • The force push won't affect anything outside of that branch, will it? I'm the only person working on this branch, but there are a ton of people working on Master and I don't want to affect any of their work. – NeonBlueHair Jul 06 '16 at 22:11
  • No, because you're specifying the branch name, that's the only one that will be pushed. Just don't type master as the branch name ;) – jonnystoten Jul 06 '16 at 22:12
  • Looks like that worked. It was a lifesaver, thanks so much! – NeonBlueHair Jul 06 '16 at 22:31
1

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.