git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Richard Kerry <richard.kerry@eviden.com>
Cc: "git@vger.kernel.org" <git@vger.kernel.org>
Subject: Re: Merge selected files or folders
Date: Thu, 21 Mar 2024 10:50:52 -0700	[thread overview]
Message-ID: <xmqqbk778oeb.fsf@gitster.g> (raw)
In-Reply-To: <PA4PR07MB7406FAC1F8C00E29979FCFF59E322@PA4PR07MB7406.eurprd07.prod.outlook.com> (Richard Kerry's message of "Thu, 21 Mar 2024 16:50:30 +0000")

Richard Kerry <richard.kerry@eviden.com> writes:

> I'd like to merge only certain files, or folders, from another
> branch.  What command or options should I be looking at to get
> this done?

If you are using the verb "merge" in the way Git uses, then there is
*no* option to do so and that is very much deliberate, as allowing
such a operation will break your history.

A "merge" commit in Git records the fact that *all* changes that
were done in each parent since the merged branches diverged have
been considered and the tree recorded by the merge commit is the
result.  Hence, if you later change your mind and "merge" other
changes from the same branch, it will result in no change at all, by
definition.

But if you are porting some changes made on another branch to the
current branch, and then planning to record the result as a regular
single parent commit, then there is no fundamental reason to forbid
such an operation.  It is what cherry-pick ought to be able to do,
even though I do not think it accepts a pathspec to limit currently.

Assuming a history of this shape:

      x---x---X (that other branch)
     /
    O---o---o---o---H	(current branch)

such a "cherry-pick" would essentially be applying all the changes
lead to X since the histories forked at O on top of H:

    $ git checkout H
    $ O=$(git merge-base X H)
    $ git diff $O X | git apply
    $ git commit -m "picked changes from branch X"

And if you want to limit the paths involved in the operation, the
"git diff" step can be given a <pathspec> to limit the changes that
are ported.

    $ git checkout H
    $ O=$(git merge-base X H)
    $ git diff $O X -- thisdir/ that/file | git apply
    $ git commit -m "picked changes from branch X"

Teaching "git cherry-pick" to accept a pathspec and natively 
perform something like the above is left as an exercise.

HTH

  reply	other threads:[~2024-03-21 17:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-21 16:50 Merge selected files or folders Richard Kerry
2024-03-21 17:50 ` Junio C Hamano [this message]
2024-03-22  9:39   ` Sergey Organov
2024-03-22  9:56     ` Konstantin Khomoutov
2024-03-22 10:27       ` Dragan Simic
2024-03-22 12:24   ` stefan.naewe
2024-03-22 17:23     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqbk778oeb.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=richard.kerry@eviden.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).