Search code examples
gitgit-rebase

git push rejected, maybe because of rebase


I have an automated Jenkins job which simply does the following:

  • pull from remoteA
  • push into remoteB

remoteA is the remote where developers actually push their code.

remoteB is never touched manually, it only receives udates trough that automated Job.

It worked well for a while, but now I get Updates were rejected because the tip of your current branch is behind its remote counterpart. when trying to push into remoteB

As far as I know, this happens when

  • (a) remoteB contains changes which remoteA doesn't contain -> I think that's not possible because no human and no other Jenkins Job touches remoteB
  • (b) someone did a rebase on remoteA. -> As I am no git professional, I am very unsure how to deal with that scenario. Hope someone here can help.

Solution

  • If you did a rebase which modified the history so you cannot push to the remote unless you wish to overwrite and change the content of your repository.

    If you still wish to push the content you must use the -f

    # FORCE overwrite of old content with the new result of you rebase 
    git push -f  
    

    In your case that the content is handled by Jenkins, a rebase can be a good reason to why it stopped working.


    How to find out if there was a rebase

    Check the diff between the 2 branches (local vs remote)

    # fetch all remotes if you have multiple ones
    git fetch --all
    
    # check the diff between the 2 branches (2 ..)
    git diff localBranch..origin/remoteBranch
    
    
    # check the diff between the 2 branches (3 ..)
    git diff localBranch...origin/remoteBranch 
    

    you can read more about git diff
    How to show uncommitted changes in Git