zfedoran
zfedoran

Reputation: 3046

How can I pull in changes to a file from a subdirectory in another git repository

I have a file in my git repository which sometimes receives bug fixes in another git repository. I would like to be able to pull those commits into my repo.

For example:

project_a/
  /.git
  /src/somefile.cpp

project_b/
  /.git
  /utils/converters/apples/bananas/src/somefile.cpp

I would like to pull in commits from project_b to project_a for somefile.cpp.

Upvotes: 1

Views: 422

Answers (2)

RaB
RaB

Reputation: 1565

In case you have files with the same name but in different folders (lets assume that you had project_b merged into a subdir) then you have to do the cherry pick in the following fashion

git cherry-pick project_b/master --strategy=subtree

Upvotes: 3

peterjmag
peterjmag

Reputation: 6179

You can add project_b as a remote and then fetch and cherry-pick from it. Try something like this:

git remote add project_b /path/to/project_b/
git fetch project_b

Now you can cherry pick from project_b. If the commit you want is at the tip of project_b's master branch, just do

git cherry-pick project_b/master

Alternatively, you can bypass the remote fetching/cherry-picking stuff and just patch directly from project_b like so:

git --git-dir=/path/to/project_b/.git format-patch -k -1 \
--stdout <commit SHA> somefile.cpp | git am -3 -k

(Adapted from this answer.)

Upvotes: 2

Related Questions