Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 378
Finglish Avatar asked Mar 20 '14 12:03

Finglish


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


5 Answers

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!

like image 107
Akash Avatar answered Oct 18 '22 21:10

Akash


I would do it this this way:

  1. Stage all unstaged changes.

    git add .
    
  2. Stash the changes.

    git stash save
    
  3. Sync with remote.

    git pull -r
    
  4. Reapply the local changes.

    git stash pop
    

    or

    git stash apply
    
like image 38
Rebecca Abriam Avatar answered Oct 18 '22 21:10

Rebecca Abriam


I had the exact same issue on my branch(lets call it branch B) and I followed three simple steps to get make it work

  1. Switched to the master branch (git checkout master)
  2. Did a pull on the master (git pull)
  3. Created new branch (git branch C) - note here that we are now branching from master
  4. Now when you are on branch C, merge with branch B (git merge B)
  5. Now do a push (git push origin C) - works :)

Now you can delete branch B and then rename branch C to branch B.

Hope this helps.

like image 30
Chetan Chandrashekar Avatar answered Oct 18 '22 23:10

Chetan Chandrashekar


This worked for me:

git pull origin $(git branch --show-current)
git push

fyi git branch --show-current returns the name of the current branch.

like image 4
Bohemian Avatar answered Oct 18 '22 22:10

Bohemian


I had the same problem. Unfortunately I was in wrong catalog level.

I tried to: git push -u origin master -> there was a error

Then I tried: git pull --rebase -> there still was a problem
Finally i change directory cd your_directory

Then I tried again ( git push) and it works!

like image 3
Paweł R Avatar answered Oct 18 '22 23:10

Paweł R