How to resolve git error: "Updates were rejected because the tip of your current branch is behind"

Finglish picture Finglish · Mar 20, 2014 · Viewed 159.5k times · Source

I recently started using Git (previously I used subversion but I am now doing some collaborative work on a project that uses bitbucket and git).

All has been going well up until today when I find a well meaning colleague has pushed changes to the Master instead of making a branch. This means that when I try to commit I get the error:

Updates were rejected because the tip of your current branch is behind

I know this should be resolved by making a pull request to re-sync things but I don't want to lose the changes I have made locally and I equally don't want to force the commit and wipe out the changes made by someone else.

What is the correct approach to allow me to merge the changes without losing either?

Answer

Akash Agrawal picture Akash Agrawal · Mar 20, 2014

If you have already made some commits, you can do the following

git pull --rebase

This will place all your local commits on top of newly pulled changes.

BE VERY CAREFUL WITH THIS: this will probably overwrite all your present files with the files as they are at the head of the branch in the remote repo! If this happens and you didn't want it to you can UNDO THIS CHANGE with

git rebase --abort 

... naturally you have to do that before doing any new commits!