2

I have two branches that diverge more than they should.

Since this has been marked as duplicate, let me clarify my question.

I already know that I can just do git push -f to force the merge. That's what I've done already several times. My question is more about the inner workings of git. If I add a new file to my branch B, I can commit it and push it without any problem. But if I git checkout branch-A somefile.txt and then git add and git push, I get the error below. Why does this kind of commit seem to always result in that error when pushing?

End edit.


To reconcile them to a common ancestor, I have to copy some files from one branch to the other. These are files that should actually have been there but got lost somehow in the process. Every time I do that using git checkout branch-A somefile.txt, commit, and push, the origin gives me this error:

"Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote branches before pushing again."

Why is that?

More detail, if needed: The two branches diverged before we started using git. Now I'm trying to sew them together. I am making sure both branches are correct and all files are identical that should be, so that I can then create a merge point that actually makes sense. They are each a branch of the same repo. In finding their common point of ancestry, I've noticed some files that should have been on both branches are missing from one or the other ( probably the result of some mistake I made when putting them under git). To fix that, I simply git checkout branch-A missingfile.txt into branch B, git add, git commit, and git push. But for some reason every time I push, I get the "out of date" error and have to do a force push. Which is fine if I know I only changed one file, but if I've changed/copied a bunch, then I always have to double check to make sure there isn't some error.

Buttle Butkus
  • 9,206
  • 13
  • 79
  • 120
  • Possible duplicate of [Cannot push to GitHub - keeps saying need merge](http://stackoverflow.com/questions/10298291/cannot-push-to-github-keeps-saying-need-merge) – kan Jan 07 '16 at 01:04
  • @kan You did some work marking this as a duplicate. But I already mention force pushing in my question. Perhaps I wasn't clear enough. I'm asking *why* this particular action results in that error message. I'm not asking for a workaround, which I already know and stated I knew in my question. Thanks. – Buttle Butkus Jan 07 '16 at 01:11
  • 2
    Have you tried git pull before? – CodeWizard Jan 07 '16 at 01:47
  • 1
    @codeWizard I was up to date with origin. I checked out 1 file from the other branch, did `git add`, `git commit`, and `git push`. Then I got the error message. Why would I need to pull again when I was just up to date? Why not just push? – Buttle Butkus Jan 07 '16 at 01:56
  • 1
    The dup question has explanations why is it happening and also mentions pull/merge, not only force push. Please try to understand: What is exactly happening? Where and why do you have diverged history? Do you see and understand your history graph? – kan Jan 07 '16 at 11:01
  • 1
    @kan I am pretty new to git and I just dived right into this major mess I'm trying to fix. I theorize that the issue is with my using `git checkout other-branch somefile.txt` to checkout the file. Whereas if I just created the file anew, there would be no warning message. I theorize that git is tracking it as some sort of partial merge? All I really wanted to do was copy a file from one branch to another, without merging any history between them yet. – Buttle Butkus Jan 08 '16 at 02:45
  • 1
    @codeWizard yes I did and I think it worked, but I find it weird to have to do it again 5 seconds after I just did it. `git pull`, `git checkout otherbranch somefile.txt`, `git add .`, `git commit`, so far so good. `git push` - error. I'm just trying to understand the logic that is happening in git here. – Buttle Butkus Jan 08 '16 at 02:47
  • 1
    @kan JakeGreen's answer in the "duplicate" question is not directly applicable. They are talking about the origin changing since the last pull. I pulled 5 seconds ago, made changes locally, and then pushed and got the error message. The origin did **not** change during that time. But perhaps I should be re-pulling rather than force-pushing anyway. This must certainly because of my checking out the file from the other branch. – Buttle Butkus Jan 08 '16 at 02:51
  • 1
    Are you pulling and pushing the same branch? git says on console what exactly it's doing, just read it carefully. Show the difference between your local branch you are trying to push from and the remote branch you are trying to push into, you could use `git log `. – kan Jan 08 '16 at 12:53

2 Answers2

1

It seems that you simply need to pull remote changes before pushing yours.

The message the server responded you is clear:

"Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote branches before pushing again."

Yosef-at-Panaya
  • 676
  • 4
  • 13
1

From the comments:

@codeWizard yes I did and I think it worked, but I find it weird to have to do it again 5 seconds after I just did it.

git pull 
git checkout otherbranch somefile.txt
git add .
git commit 

so far so good.

git push - error. 

I'm just trying to understand the logic that is happening in git here

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167