I just finished working on a piece of code. Wanted to push and got the already famous:
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.
Now I've seen this question posted several times here, e.g.
Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g
Updates were rejected because the tip of your current branch is behind
According to the specific case, the solution is either to
git pull
, so the remote changes are merged on to my local work, ORgit push -f
, a force push to update the remote (origin) branch.Now, it has been a while I haven't worked on this branch. I don't necessarily want to merge the remote changes onto my current work! Nor do I know if I can safely force the update on the origin branch...
How can I just see the differences and decide which is best for my case?
A commit gets rejected and causes a failed to push some refs to error because the remote branch contains code that you do not have locally. What this means is that your local git repository is not compatible with the remote origin. Based on the above, your local machine is missing commits C and D.
Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch. Continue to perform Git commits locally on the new branch. Simply use a git push origin command on subsequent pushes of the new branch to the remote repo.
in order to see the differences, first you need to fetch the commits from the origin repository:
git fetch origin
Now you can see the diffs (Assuming you are on the master branch)
git diff HEAD..origin/master
Now you are armed with the knowledge you seek to decide to merge
or rebase
before push
ing your changes.
I had this happen to me recently when I created a new branch with git checkout -b feature/abc
, committed some changes, and then tried git push --set-upstream origin feature/abc
it to create a pull request for review. The error occurred because the remote branch already existed when I thought I was defining the branch locally. Deleting the remote branch resolved the issue and my push succeeded.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With