ChatGPT解决这个技术问题 Extra ChatGPT

Git error on commit after merge - fatal: cannot do a partial commit during a merge

I ran a git pull that ended in a conflict. I resolved the conflict and everything is fine now (I used mergetool also).

When I commit the resolved file with git commit file.php -m "message" I get the error:

fatal: cannot do a partial commit during a merge.

I had the same issue before and using -a in commit worked perfectly. I think it's not the perfect way because I don't want to commit all changes. I want to commit files separately with separate comments. How can I do that? Why doesn't git allow users to commit files separately after a merge? I could not find a satisfactory answer to this problem.

And you know, searching for git perform "full commit" returns nearly nothing useful. Not one relevant document from Git's man pages. This is such a miserable tool.
@Torek - talk about another mess created by Git... Why is it so god damn difficult to checkout a conflicted file from another branch so the conflict is averted, add it to this branch, and then commit it??? And WTF is a partial commit? I can't find any documentation on it... Users are forced to try the guesses below...
How did you do it? I have a conflict and I can't do anything

M
MikaelHalen

I found that adding "-i" to the commit command fixes this problem for me. The -i basically tells it to stage additional files before committing. That is:

git commit -i myfile.php

What does Stage additional files mean?
@jcalfee314 staging in git is to prepare the file for the commit. In this particular case it stages the file via command line before committing. The -i flag is used mostly for when you are concluding a merge. You could read more about the commit flags here.
@jcalfee314 I checked the documentation and it says "Before making a commit out of staged contents so far, stage the contents of paths given on the command line as well. This is usually not what you want unless you are concluding a conflicted merge". My guess is that under this condition, there is some kind of inconsistency in the staging area that can't be solved by git add, which makes git commit fail. Adding -i would tell git to add and commit at the same time. I'm still not sure why but it seems to make sense.
For noobs like myself, you will also get this same error if you try a message that includes spaces without quotes. Eg. [git commit -m one two three] Correct: [git commit -m "one two three"]
@Skychan's comment was the ticket for me.
P
Pratip Ghosh
git commit -am 'Conflicts resolved'

This worked for me. You can try this also.


Amazing, I did not know this!
This adds all modified files to the commit, even ones that are unstaged which may not be desirable. Users may want to leave off the 'a' flag
This is exactly what the user is asking not to do, which is to avoid committing all files.
E
Emile Bergeron

You can use git commit -i for most cases but in case it doesn't work

You need to do git commit -m "your_merge_message". During a merge conflict you cannot merge one single file so you need to

Stage only the conflicted file ( git add your_file.txt ) git commit -m "your_merge_message"


P
Post Self

I got this when I forgot the -m in my git commit when resolving a git merge conflict.

git commit "commit message"

should be

git commit -m "commit message"

ah ah! I forgot again!
P
Paul Price

You probably got a conflict in something that you haven't staged for commit. git won't let you commit things independently (because it's all part of the merge, I guess), so you need to git add that file and then git commit -m "Merge conflict resolution". The -i flag for git commit does the add for you.


Not in 1.9.0 — the commit -i worked, but not git add; git commit
S
Sushil Adokar

Looks like you missed -m for commit command


This was my case too. Unfortunately I had to read all above before I find the solution... I was using it like this git commit "message" . you need -m in front of "message": git commit -m "message"
A
Asim Jalis

As the error message says you cannot do a partial commit after a merge. Instead of committing just file.php you should commit all the changes.

This should work.

git commit -m "Fixing merge" 

Thanks, it helped
Worked for me as-it-is
A
Ahmed Samir

go to your project directory display hidden files (.git folder will appear) open .git folder remove MERGE_HEAD commit again if git told you that git is locked go back to .git folder and remove index.lock commit again everything will work fine this time .


Great! That'd work for me. By the way if on MacOS, from the terminal you could have call ` open .git`, witch will display the '.git' content in the Finder
This works for me too. This should be accepted as answer.
M
Moyshe Zuchmir

Your merge stopped in the middle of the action. You should add your files, and then 'git commit':

git add file_1.php file_2.php file_3.php git commit

Cheers


Z
Zds

If you just want to ditch the whole cherry-picking and commit files in whatever sets you want,

git reset --soft <ID-OF-THE-LAST-COMMIT>

gets you there.

What soft reset does is it moves the pointer pointing to current HEAD to the commit(ish) you gave but does not alter the files. Hard reset would move the pointer and also revert all files to the state in that commit(ish). This means with soft reset you can clear the merge status but keep the changes to actual files and then commit or reset them each individually per your liking.


Could you explain this more? This is probably what I need, but I don't follow how it would work... All the answers here are just 'add the files, then commit!', but that's so trivially obvious; the reason I'm here is I don't want to add those files before I commit. -_-;
That helps. Thanks. :)
K
Ketan Patel

Sometimes during the merge, if conflicts arise and there are deltas that need manual resolution. In such case fix the manual resolution for the files mentioned.

Now if you issue,

git status Lib/MyFile.php

You will see output like

On branch warehouse
Your branch and 'origin/warehouse' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

    modified:   Lib/MyFile.php

Since, you already staged the commit, you need just issue

git commit

And your commit will be done without any issue.


z
zx485

After reading all comments. this was my resolution: I had to "Add" it again than commit:

$ git commit -i -m support.html "doit once for all" [master 18ea92e] support.html

J
Justin

For myself this happened in SourceTree when I tried to commit a merge before resolving all of the files. I then marked the last file resolved and yet it still gave me this error when trying to commit. I closed SourceTree and reopened it, and then it committed fine.


thanks, but unfortunately this didn't work for me. Annoyingly, I had to commit a view-private as well as the merge before it would let me commit the merge.
E
Emile Bergeron

I solved this with a completely different approach, using only Xcode's Source Control.

Background: Another team Pushed changes to the remote Git repository (via Beanstalk). On my end, the .xcodeproj files came in under different directory, and the changes didn't take. Later, when I tried to commit, I received a Tree Conflict error in Xcode.

https://i.stack.imgur.com/eJEcl.png

Being nearly impossible to correct using Xcode, I replaced the .xcodeproj file with a downloaded version from the Git server. The result... the Xcode project appeared to clear up, however all the updates from the corrupt Pull were showing up as changes I made and were Staged for a Commit.

https://i.stack.imgur.com/4N2si.png

However when trying to Commit, I received the same "fatal: cannot do a partial commit during a merge" error, discussed here.

Here's how I solved the problem... (Now, understand that I'm a rookie programmer, so I could lack some understanding... but my ignorance led me to find another way to do this.) First, I Cloned my master Branch into a secondary Branch and switched to that branch. Then I created a Working Copy and placed the directory to that working copy outside of the original project directory. (I don't know if this was necessary, but its what I did as I read other troubleshooting techniques.) Then I switched branches to the master, where I realized all my Staged files (changes to Commit) were gone. To make sure all the files were updated to the latest changes made by the other party, I created a new branch called ThirdBranch, which duplicated all files, Pushed it to the Git Server and let Beanstalk compare my server version of the master branch to the ThirdBrach branch I just Pushed (line by line), and all the changes by the other party were present on my Xcode. This meant that my master repository and the Git master repository were the same, which verifies that I solved the problem using Xcode, only.

Don't ask me how, beyond what I just described... and certainly fill in the gaps I left out. I'm new at this and I don't understand everything. Maybe an experienced programmer can separate the irrelevant info from the relevant and recreate this technique more clearly, which is in part why I'm posting this.

This is a duplicate answer to duplicate question as at: Failed Xcode Git Merge is stuck


Please do not post duplicate answers. While the underlying problem may be the same, your answer is for a much more specific problem and only clouds the answers to this general question. I think in this case a comment to the question in which you link to your original answer is more appropriate.
P
Peter Csala

Simply git commit -m "comment" should work after resolving conflicts..


T
Talljoe

During a merge Git wants to keep track of the parent branches for all sorts of reasons. What you want to do is not a merge as git sees it. You will likely want to do a rebase or cherry-pick manually.


I never used rebase or cherry-pick before, I just ran through the manual now, so what would you suggest, "git rebase master" after merging conflicts will work?
It's a parallel workflow. See stackoverflow.com/questions/804115/git-rebase-vs-git-merge Basically, if you want the "merge" to be separate commits you instead rebase the source branch onto the end of the target branch.
Just git add each individual file then commit without -a.
you did not actually answer the question but rather simply gave more to search for. Now we need to know "what is cherry picking" and "what is rebase".
I wonder why this answer was down-voted. I always had the child's curiosity when somebody tells me things I never knew before. As I commented above, now I know about cherry pick and rebase. Wasn't that progressive/helpful?
C
Community

git commit -i -m 'merge message' didn't work for me. It said:

fatal: No paths with --include/--only does not make sense.

FWIW, I got here via this related question because I was getting this message:

fatal: You have not concluded your merge (MERGE_HEAD exists).

I also tried mergetool, which said No files need merging. Very confusing! So the MERGE_HEAD is not in a file that needs merging-??

Finally, I used this trick to add only the modified files (did not want to add all the files in my tree, since I have some I want to keep untracked):

git ls-files -m | xargs git add

Then I was finally (!) able to commit and push up. It sure would be nice if git gave you better hints about what to do in these situations.


c
cgr

If it is in Source Tree, we should explicitly mark a file as resolved after the conflicts are resolved. Select file that was just resolved to no conflicts. Then Actions -> Resolve Conflicts -> Mark Resolved. If you have multiple files, do the same for all. Commit now.


l
luky

If you are using Source tree or other GUI ensure all files are checked (after merge).