git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Behaviour of git apply --directory

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 22, 2011, 18:28 UTC
Message-ID
<7vfwktwbbb.fsf@alter.siamese.dyndns.org>
In-Reply-To
<E21BC0F8-2DF0-44AB-B18A-013451CDB670@sentex.net>
Andrew Berry <andrewberry@sentex.net> writes:
Show 5 quoted lines
> The behaviour of git apply --directory seems a little confusing. It
> looks to be dependent on the current directory, but I can't use relative
> paths to apply a patch in one directory to a sibling directory. Absolute
> paths don't work either. I'd expected the parameter to either be
> relative to the git repository root, or to expand relative directories.

I do not think that parameter does not have anything to do with your cwd. As the documentation says:

  --directory=<root>::
          Prepend <root> to all filenames.  If a "-p" argument was also passed,
          it is applied before prepending the new root.
  For example, a patch that talks about updating `a/git-gui.sh` to `b/git-gui.sh`
  can be applied to the file in the working tree `modules/git-gui/git-gui.sh` by
  running `git apply --directory=modules/git-gui`.

the parameter is just a fixed string that is used to modify the path that appears in the patch before it gets applied, and has nothing to do with your current (or previous for that matter) working directory.

Suppose you have a patch that tries to update "COPYING". Such a patch generated by Git would look like this:

    diff --git a/COPYING b/COPYING
    index 536e555..ee559b1 100644
    --- a/COPYING
    +++ b/COPYING
    @@ -1,3 +1,4 @@
    +GPL GPL GPL
      Note that the only valid version of the GPL as far as this project
      is concerned is _this_ particular version of the license (ie v2, not

Further suppose that you have already rearranged your project so that that file appears in licenses/gpl directory, and your $(cwd) is licenses/ subdirectory of your working tree.

You would give --directory=licenses/gpl/ without passing any custom -p parameter. This internally turns the patch being applied into something like:

    diff --git a/licenses/gpl/COPYING b/licenses/gpl/COPYING
    index 536e555..ee559b1 100644
    --- a/licenses/gpl/COPYING
    +++ b/licenses/gpl/COPYING
    @@ -1,3 +1,4 @@
    +GPL GPL GPL
      Note that the only valid version of the GPL as far as this project
      is concerned is _this_ particular version of the license (ie v2, not

Because the patch application in git is always relative to the top level of your working tree no matter where you are, this applies to the path you intended it to.

Here is a sample transcript to try it yourself.
    $ (echo GPL GPL GPL; cat COPYING) >x && cat x >COPYING
    $ git diff >P.diff
    $ git checkout COPYING
    $ mkdir -p licenses/gpl
    $ git mv COPYING licenses/gpl
    $ cd licenses
    $ git apply -v --directory=licenses/gpl ../P.diff
    Checking patch licenses/gpl/COPYING...
    Applied patch licenses/gpl/COPYING cleanly.
    $ git diff
    diff --git a/licenses/gpl/COPYING b/licenses/gpl/COPYING
    index 536e555..ee559b1 100644
    --- a/licenses/gpl/COPYING
    +++ b/licenses/gpl/COPYING
    @@ -1,3 +1,4 @@
    +GPL GPL GPL
      Note that the only valid version of the GPL as far as this project
      is concerned is _this_ particular version of the license (ie v2, not
Previous: Andrew BerryNext: Andrew Berry
Message 2 of 3 in “Behaviour of git apply --directory”
  1. Andrew BerryAug 22, 2011
  2. Junio C HamanoAug 22, 2011
  3. Andrew BerryAug 24, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.