Martin AJ Martin AJ -4 years ago 131
Git Question

Why both pull and push throw error in git?

I have to push my new changes to the repository. I have added

and created a commit for it. But when I enter

$ git push origin master

It throws an error and says (as a hint)
first. But when
, again it throws an error and talks about
. Honestly I'm confused. What should I do now?

enter image description here

Answer Source

Pushing your branch is not working because your local master and the version on the remote have diverged. This means that, while each of these branches share a common ancestor commit, since that point each branch has added different subsequent commits. Here is a small sample diagram showing what the local and remote master branches might look like:

remote: ... A -- B -- C
local:  ...        M -- N

Git is rejecting the push because it doesn't know how to apply the M commit on top of the remote C commit. The bases are different, so you just get an error telling you to pull to correct this.

Your hunch to do a git pull is correct, but the branch state master|MERGING means that you are already in the middle of another merge. Possibly, you pulled once already and now there are merge conflicts which need to be resolved. You can locate the files which are in conflict by doing git status. Resolve all conflicts, and the git add each file, followed by git commit. After doing these steps, git push should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download