Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 920
transient_loop Avatar asked Jul 25 '17 03:07

transient_loop


People also ask

Why is my git push rejected?

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.

How do you push changes to upstream branch?

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.


Video Answer


2 Answers

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.

like image 197
Ben Avatar answered Oct 17 '22 16:10

Ben


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.

like image 44
djb Avatar answered Oct 17 '22 18:10

djb