249

I forgot to git pull my code before editing it; when I committed the new code and tried to push, I got the error "push is not possible".

At that point I did a git pull which made some files with conflict highlighted. I removed the conflicts but I don't know what to do from here.

I tried to git commit again but it says the "commit is not possible because you have unmerged files":

error: Committing is not possible because you have unmerged files.
pkamb
  • 33,281
  • 23
  • 160
  • 191
Kiarash
  • 7,378
  • 10
  • 44
  • 69
  • 1
    You might want to change your git workflow so that you never arrive in this situation in the first place. I don't have time to elaborate on it right now, but what I have in mind includes either [git merge -X theirs](https://stackoverflow.com/a/3364506) or [git pull -X theirs](https://stackoverflow.com/a/21777677). - Probably not both, but I am not sure. – Henke Dec 17 '20 at 08:13

7 Answers7

347

If you have fixed the conflicts you need to add the files to the stage with git add [filename], then commit as normal.

jonnystoten
  • 7,055
  • 2
  • 28
  • 36
  • 3
    What if those are the files I have not been working on? Should I then still add them? What is the best way to deal with it? – R11G Jun 19 '13 at 12:50
  • 2
    I had to do this for a file that was deleted in a branch that I was merging into the current branch. Git flagged it as a conflict, so I had to delete the file locally then `git add the_file` in order to commit the merge. – Brendon Muir Sep 15 '16 at 23:14
  • how to find list of conflicts? – polina-c Aug 08 '18 at 17:32
  • 33
    `git status` will show you the files that have conflicts – jonnystoten Aug 08 '18 at 18:03
  • 1
    @jonnystoten thanks for your comment! I used `git status` and found a file that said `both deleted`. WebStorm UI didn't show the problem, just said it couldn't commit a merge. Resolved! – Yuri Predborski Mar 28 '19 at 08:45
  • I think the answer "git add" is correct, but not complete. If I have a merge conflicts during cherry-pick and do "git add" + "git commit" as suggested I get the error: "fatal: cannot do a partial commit during a cherry-pick.". In this case I have to do this: "git add" + "git cherry-pick --continue" + "git push". – Alexander Samoylov May 09 '19 at 14:23
  • If you do not want to add that file in your commit and you are getting the error even after resolving the commit, simply stage and un-stage that file fixed the error for me. – shubham rajput Jun 28 '21 at 06:47
69

You need to do two things. First add the changes with

git add .
git stash  

git checkout <some branch>

It should solve your issue as it solved to me.

Prabhakar
  • 6,458
  • 2
  • 40
  • 51
  • 4
    It is not required only git add . would suffice – AbaEesa Sep 19 '18 at 15:11
  • 3
    Also this is a heavy hitter if you're not wanting to `git add` all your working files – courtsimas Dec 11 '18 at 17:37
  • 3
    Do not run "git add ." unless you are ready to add all local files to your GitHub repo. – jtb Feb 18 '21 at 00:57
  • please don't use stash .. it we remove all u changed – ElsayedDev Jul 18 '21 at 08:55
  • This way you will get rid of the blocking message, but resolved conflict changes will also be rolled back. If you want to keep them - copy `src` folder, do what's in this answer, and paste `src` back. – Zon Aug 25 '21 at 07:27
13

You can use git stash to save the current repository before doing the commit you want to make (after merging the changes from the upstream repo with git stash pop). I had to do this yesterday when I had the same problem.

muman
  • 141
  • 8
11

This error occurs when you resolve the conflicts but the file still needs to be added in the stage area. git add . would solve it. Then, try to commit and merge.

greg-449
  • 109,219
  • 232
  • 102
  • 145
Priyanka Arora
  • 409
  • 4
  • 10
5

Since git 2.23 (August 2019) you now have a shortcut to do that: git restore --staged [filepath]. With this command, you could ignore a conflicted file without needing to add and remove that.

Example:

> git status

  ...
  Unmerged paths:
    (use "git add <file>..." to mark resolution)
      both modified:   file.ex

> git restore --staged file.ex

> git status

  ...
  Changes not staged for commit:
    (use "git add <file>..." to update what will be committed)
    (use "git restore <file>..." to discard changes in working directory)
      modified:   file.ex

macabeus
  • 4,156
  • 5
  • 37
  • 66
  • is there anyway to apply this command to all files, since I've quite a lot of files needing merge, and applying this command to each file by name will be quite hectic task – Lint Aug 15 '22 at 07:14
  • 1
    @Lint You can apply for all files within a path. For example: `git restore --staged .` or `git restore --staged folder/` – macabeus Aug 16 '22 at 07:49
2

I've had a similar issue which boiled down to removing files under "unmerged paths"

Those files had to be removed using git rm

Yann VR
  • 486
  • 5
  • 9
0

So from the error above. All you have to do to fix this issue is to revert your code. (git revert HEAD) then git pull and then redo your changes, then git pull again and was able to commit or merge with no errors.

Brydenr
  • 798
  • 1
  • 19
  • 30
appdesigns
  • 114
  • 1
  • 11