git: "Updates were rejected because the tip of your current branch is behind.." but how to see differences?

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, OR
  • git 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?


Solution 1:

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 pushing your changes.

Solution 2:

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.