0

I'm trying to use git am to apply a patch created using git format-patch. There were a few conflicts. As suggested in several other questions, I'm actually using git am --reject, so that it patches the files it can (most of them), and generates .rej files for the rest. All right so far.

For each of the generated .rej files, I'm making changes by hand, and then using git add to indicate the files I've fixed. All right so far.

Finally, I'm running git am --resolved to complete the process.

But! The commit it creates contains only the (few) files for which I manually resolved conflicts and explicitly git added them. All the others, that it successfully patched, are uncommitted, still showing up when I do a git diff.

How can I convince git am --reject to do a git add on the files it successfully patches, so that I don't have to? (I figured out a way to do all the extra git adds, explicitly, myself, but it's obviously a big and seemingly unnecessary nuisance — in my case, there were 60 of them.)

git version 1.8.3.1, if it matters.

6
  • 1
    I think there have been a couple of git releases after the one you are using. Can you try with a newer version? That will probably help. Commented Oct 2 at 3:52
  • @eftshift0 "…a couple of git releases…" A couple, yeah! :-D A couple of major git releases! Commented Oct 2 at 11:56
  • I know it's an old version, but it is not convenient for me to upgrade the machine in question, which is why I asked the question, hoping someone might know an answer. (And by "not convenient", I mean "basically not possible". Tedious as it is to explicitly git add all the patched files that git am forgot to, that's less work than upgrading.) Commented Oct 2 at 14:24
  • 2
    @SteveSummit What about git stash before git am --reject and git add . after to add all patched files (and git stash pop after all)? Commented Oct 2 at 15:50
  • @pmd Yeah, I thought of that. It's a good suggestion. My problem is that I don't trust git stash, and I don't trust open-ended git add (having had too many bad experiences with both). (But that's my issue.) Commented Oct 2 at 15:53

1 Answer 1

0

It's still unclear why git am doesn't add files to the index if there are conflicts. The procedure I ended up with was to resolve conflicts, then manually add everything. The working copy of the patch file contains the necessary information. There are probably better, more git-savvy ways of doing this, but because I'm an old Unix weenie, I used a shell pipeline, something like this:

grep ^diff .git/rebase-apply/patch | awk '{print $3}' | sed 's;^a/;;' | while read f do git add --all $f done 

(That file .git/rebase-apply/patch is the one that git am --reject identified as the "copy of the patch that failed".)

This grabs the diff lines from the failed patch, grabs the filenames (column 3) from each diff line, strips off the leading "a/", and finally runs git add --all on each pathname. The --all option is needed in case a diff actually represents a deletion.

After that I could run git am --resolved to complete the process.

I'm the first to admit that this is far from the most elegant way of doing things, but it worked, and it will have to do, at least until a better answer comes along.

In the comments @phd suggested a theoretically equivalent but somewhat simpler technique involving git stash.

It's also possible that none of this is necessary with newer versions of git, but I haven't been able to explore that possibility.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.