1

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) pull first. But when pull, again it throws an error and talks about fetch. Honestly I'm confused. What should I do now?

enter image description here

Martin AJ
  • 6,261
  • 8
  • 53
  • 111
  • Worst case scenario where you want to push your changes, you can use `-f` flag. Example: `git push -f origin master`. In your case, I believe, you didn't updated your branch with respect to master after committing the changes and hence its not allowing you to push. `-f` is a force flag that forces the `push` command. – Milan Chheda Jun 10 '17 at 10:10
  • Using `git pull` runs `git fetch` and then runs `git merge`. The fetch step is working, but the merge step is failing because you're already doing a merge that you have neither finished nor aborted. I recommend avoiding `git pull` entirely until you understand both of these other commands. (Fetch is easy: merge is the tricky one, as you are already seeing.) – torek Jun 10 '17 at 16:52
  • Possible duplicate of [You have not concluded your merge (MERGE\_HEAD exists)](https://stackoverflow.com/questions/11646107/you-have-not-concluded-your-merge-merge-head-exists) – max630 Jun 11 '17 at 06:49

4 Answers4

0

It's because you have encountered some merge conflict, and still haven't resolved it yet.

git checkout -f # or git stash if you want to preserve the changes
git checkout master # just to make sure some rebase is not going on
git pull origin master
# Now resolve merge conflicts, add the files and commit.
git push origin master
hspandher
  • 15,934
  • 2
  • 32
  • 45
0

Run git status and you should see unstashed files. You could either save them for now with git stash save <patch_name> or commit those changes if you want . Once thats done, git pull to get the latest changes in master as your tip is behind remote/master followed by git push

Mayur Nagekar
  • 813
  • 5
  • 13
0

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

You can check the status first to see if there have any un-staged or conflict files:

git status

After fix that and commit it, you can pull again from origin master.

git add -A
git commit
git pull origin master
Nick Tsai
  • 3,799
  • 33
  • 36