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

13

82

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?

Promycelium answered 20/3, 2014 at 12:24 Comment(3)
git stash your changes (if they are uncommitted), sync with remote, reapply changes with git stash popRancourt
Unfortunately, I had already made a couple of local commits before attempting to push and realising the problem. How should I deal with these?Promycelium
for pushing heroku if you get this error then do https://mcmap.net/q/54823/-error-with-39-git-push-heroku-master-39-commandElfie
G
117

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!

Gissing answered 20/3, 2014 at 12:47 Comment(7)
This is what I needed, but the stash approach is also useful so I voted up Tim Castelijns comment as well.Promycelium
This deletes my local changes and makes them like the one in the repo. Good thing I put my project into a .zip folder before I did it :)Attic
Adding a comment for my experience. I had to add git pull --rebase <remote> <branch> and then fix merge conflicts. Then git add and finally git rebase --continue. Something may be off with your local and remote branches that needs to be fixed.Anaesthesia
after doing git pull --rebase origin master it said Current branch my_branch is up to date.. I'm happy. But when I did try again to push it saying same issue "hint: Updates were rejected because the tip of your current branch is behind" Any suggestion ?Uncharitable
@AtlanteAvila Thanks so much for this, this is what helped me. First pull, then fix merge conflicts, then add, then rebase --continue. awesome.Dior
is it git pull origin main --rebase? Because git pull --rebase doesn't seem to work to me because it asks me about the branchKegler
@EdisonPebojot The short version works if your local branch is set to track the remote branch. The remote branch is then called the local's upstream branch. You can read more about it devconnected.com/how-to-set-upstream-branch-on-gitGissing
O
25

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
    
Often answered 20/3, 2014 at 12:41 Comment(2)
This is also VERY DANGEROUS! git pull -r will mean that any commits you have done relative to the head of the remote branch will be utterly destroyed! Your "stash" will only be changes relative to your last local commit! The OP says specifically in a comment "I had already made a couple of local commits before attempting to push" ...Modulator
Using git pull -r is a matter of personal preference as I have indicated. And I would use it on scenarios when doing so will not result to merge conflicts or commit destruction as @mike mentioned. Otherwise, git pull would suffice. The beauty of using git pull -r, in my opinion in the above situation when it does not result to merge conflict, is that it puts my local commits on top (i.e. cleaner log history) by not creating the extra automaticmergecommit which I find as 'noise' and avoidable.Often
B
15

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.

Bankston answered 1/5, 2019 at 11:39 Comment(1)
What if you merge the two ie B & CSingles
K
7

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.

Krugersdorp answered 12/1, 2021 at 6:41 Comment(0)
F
3

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!

Figge answered 27/6, 2017 at 13:9 Comment(0)
A
3

This issue occurs when someone has commited the code to develop/master and latest code has not been rebased from develop/master and you're trying to overwrite new changes to develop/master branch

Solution:

  • Take a backup if you're working on feature branch and switch to master/develop branch by doing git checkout develop/master
  • Do git pull
  • You will get changes and merge conflicts occur when you have made changes in the same file which has not been rebased from develop/master
  • Resolve the conflicts if it occurs and do git push,this should work
Anglicanism answered 1/7, 2020 at 7:50 Comment(0)
G
2

I was able to overcome this issue with the following Visual Studio 2017 change:

  1. In Team Explorer, go to Settings. Go to Global Settings to configure this option at the global level; go to Repository Settings to configure this option at the repo level.
  2. Set Rebase local branch when pulling to the desired setting (for me it was True), and select Update to save.

See: https://learn.microsoft.com/en-us/vsts/git/concepts/git-config?view=vsts&tabs=visual-studio#rebase-local-branch-when-pulling

Guiltless answered 20/7, 2018 at 16:29 Comment(0)
B
2

I have used

git push origin master

After update refusing, I looked up at a history:

git log --oneline --all

My HEAD -> master was above origin/master.

But I used forcing, and it was sufficient:

git push --force-with-lease origin master

And the heads are together again...

Balm answered 19/2, 2022 at 21:14 Comment(1)
This will basically discard what the colleague did. It doesn't respond to the questionChromo
C
1

You are not currently on a branch. To push the history leading to the current (detached HEAD) state now, use

git push origin HEAD:<name-of-remote-branch>
Clomb answered 25/8, 2020 at 6:58 Comment(1)
thanks! this is what i need coz i'm in a different branch and want to push to another branch without switching.Blalock
L
1

I had the same issue.

Fix: git pull origin {branch-name} sorted all.

Ref: There is no tracking information for the current branch

Lazulite answered 30/8, 2021 at 2:8 Comment(0)
S
1

This worked for me and i will recommend it to you. if you are on the local branch that you have commited, try renaming the branch with this git command

git branch -m <new_name>

then push again with

git push --set-upstream origin <new_name>
Spawn answered 15/5, 2022 at 19:3 Comment(0)
K
-1

I had this issue and I realized that .gitignore file is changed. So, I changed .gitignore and I could pull the changes.

Komsomol answered 5/11, 2020 at 13:19 Comment(0)
M
-1

i use git pull then i push the changes (git push) and commit (git commit -m "message")

  1. git init
  2. git pull //fetch changes u make in your repo that are not yet merged in your local machine code.
  3. git push // push the changes you makes from your local machine
  4. git commit -m ""
Moffit answered 18/10, 2023 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.