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:
- Clearing special permissions from folders in a branch
- Delphi: Moving away from VSS
- Source control structure for prototype and real implementation
- Listing full commit history of a particular file
- Commit early, commit often - does it mean “push” early, often in GIT?
- How to list branches that contain a given commit?
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.
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.