threads / discuss / 27824

"git apply" can't add new files to repo ?

Subject: "git apply" can't add new files to repo ?

## tl;dr

6 messages between Jul 15, 2011 and Jul 15, 2011.

replies: 5people: 3as markdown or json

Yang· Jul 15, 2011, 19:02 UTC · lore

I created a patch by "git format-patch", and this patch includes some newly-added files.

when I got back to the original branch, and apply this patch, the new files appear, but they are not added to git repository, so I have to manually find all the new files and do "git add" for each of them.

I guess there should have been a way to automatically do "git add" for all the new files in the patch, how could I do that?

thanks a lot Yang

Carlos Martín Nieto· Jul 15, 2011, 19:34 UTC · re: Yang · lore

Re: "git apply" can't add new files to repo ?

On Fri, 2011-07-15 at 12:02 -0700, Yang wrote:
Show 12 quoted lines
> I created a patch by "git format-patch", and this patch includes some
> newly-added files.
> 
> when I got back to the original branch, and apply this patch, the new
> files appear, but they are not
> added to git repository, so I have to manually find all the new files
> and do "git add" for each of them.
> 
> 
> I guess there should have been a way to automatically do "git add" for
> all the new files in the patch,
> how could I do that?

git-apply is just a wrapper around the patch(1) utility. As you have created a patch for use in an email (as that's the point of using format-patch, so you have both the commit message and diff in one place), you should use git-am, which takes care of all the "magic". If you want to change what the patch does, you just need to use git commit --amend

Cheers,
   cmn
-- 
Carlos Martín Nieto        http://www.cmartin.tk

"¿Cómo voy a decir bobadas si soy mudo?" -- CACHAI
Carlos Martín Nieto· Jul 15, 2011, 20:21 UTC · re: Junio C Hamano · lore

Re: "git apply" can't add new files to repo ?

On Fri, 2011-07-15 at 13:13 -0700, Junio C Hamano wrote:
Show 5 quoted lines
> Carlos Martín Nieto <cmn@elego.de> writes:
> 
> > git-apply is just a wrapper around the patch(1) utility.
> 
> Huh?

That was a thinko; I've always considered git-apply to be little more than patch(1), but it does look like it does much more. I should have re-looked at the man page earlier.

Sorry for the noise,
   cmn
Junio C Hamano· Jul 15, 2011, 19:39 UTC · re: Yang · lore

Re: "git apply" can't add new files to repo ?

Yang <teddyyyy123@gmail.com> writes:
Show 7 quoted lines
> I created a patch by "git format-patch", and this patch includes some
> newly-added files.
>
> when I got back to the original branch, and apply this patch, the new
> files appear, but they are not
> added to git repository, so I have to manually find all the new files
> and do "git add" for each of them.
You are doing and thinking wrong.

$ git apply patch $ git commit

would not add any new contents to existing paths either.

$ git apply --index patch $ git commit

would be the way, but that is why we have "git am".
Yang· Jul 15, 2011, 23:00 UTC · re: Junio C Hamano · lore

Re: "git apply" can't add new files to repo ?

thanks all for your replies.
yang
On Fri, Jul 15, 2011 at 12:39 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 22 quoted lines
> Yang <teddyyyy123@gmail.com> writes:
>
>> I created a patch by "git format-patch", and this patch includes some
>> newly-added files.
>>
>> when I got back to the original branch, and apply this patch, the new
>> files appear, but they are not
>> added to git repository, so I have to manually find all the new files
>> and do "git add" for each of them.
>
> You are doing and thinking wrong.
>
> $ git apply patch
> $ git commit
>
> would not add any new contents to existing paths either.
>
> $ git apply --index patch
> $ git commit
>
> would be the way, but that is why we have "git am".
>

← back to recent threads