Can't push to a repo after rebase

Advertisements

I’m having some trouble with git rebase. I have forked a public repository. In my fork I have a master branch and a feature branch. I have developed and committed code in feature and now I create a pull request directly into the upstream repository.

But, by the time I am ready to create a pr, the upstream master has already merged new commits. So I need to update my local repo before the pr can go through. To do this I checkout into master and run gh repo sync origin my-github/the-repo. I then git pull. After I checkout to feature and run rebase master. This works smoothly, no problems here. The problem is when I try to push changes to the remote feature. I get the following error:

 ! [rejected]            squares-of-id -> squares-of-id (non-fast-forward)
error: failed to push some refs to 'https://github.com/my-github/the-repo'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Can someone help explain why my local ends up behind my remote? And hopeful provide a fix?

>Solution :

By rebasing you rewrite the history and as a result your local history does not match anymore with the upstream’s history. Public branches should never be rewritten as that might cause lots of problems downstream.

However, if this is your personal branch it is fine and you can force the server to accept your commits/history by invoking git push --force-with-lease

Leave a ReplyCancel reply