43

I use Git Subtree like below:

git subtree add --prefix=directory_destination_path --squash git@bitbucket.org:kicaj/projectname.git master

But in path: directory_destination_path copy all repo from projectname.git

How to copy to directory_destination_path only subdirectory or only some file from projectname.git ?

EDIT:
One more question:
How to update (automatic) files changes in both repositories were still the same? It is possible?

jordelver
  • 8,292
  • 2
  • 32
  • 40
kicaj
  • 2,881
  • 5
  • 42
  • 68
  • Possible duplicate of https://stackoverflow.com/questions/39479154/how-can-i-split-a-single-file-from-a-git-repo-into-a-new-repo that has an answer about extracting history of a single file using `git fast-export`. – Velkan Apr 20 '22 at 14:42

3 Answers3

31

If I understand, you seem to want to only merge in a certain directory of a different repository, and you want it to be a subtree in your repository. I am going to call the directory of interest in the project.git path_of_interest_in_project and call the destination in your repo directory_desination_path.

Try adding the remote project.git as a remote, then checking out one of its branches locally. Then use git-subtree split to split out just the directory of project.git you are interested in. After that merge it into your repo using subtree merge.

git remote add project git@bitbucket.org:kicaj/projectname.git
git branch project_master project/master

The branch project_master should now store the entire history of your project.git repo.

Then you'll need to use the git-subtrees-split process.

git checkout -f project_master
git subtree split --squash --prefix=path_of_interest_in_project -b temp_branch

There should now be a branch called temp_branch containing just the directory you are interested in. Now you can perform a git-subtree-merge to bring it all into your repo.

git checkout -f master
git subtree merge --allow-unrelated-histories --prefix=directory_destination_path temp_branch

This should merge in the temp_branch into your master branch.

Bernd
  • 112
  • 7
eddiemoya
  • 6,713
  • 1
  • 24
  • 34
  • `git subtree split squash --prefix=path_of_interest_in_project temp_branch` always return: does not exist; use git subtree add – kicaj Mar 11 '14 at 21:20
  • I change `path_of_interest_in_project` to `Model/Behavior` - there are directories – kicaj Mar 11 '14 at 21:21
  • I would like use `repo1/Model/Behavior/file.php` to `repo2/Model/Behavior/file.php` They should always the same content regardless of which I change – kicaj Mar 11 '14 at 21:22
  • I had a typo. It should be `--squash` – eddiemoya Mar 11 '14 at 21:48
  • 2
    As far as them being in sync, theres no built in way to just automatically keep these files (only) in sync. You would have to write a script to handle it all. An easier thing might be to track these files as independent repo's and use them as subtress in both projects. This is simply not a good workflow overall. – eddiemoya Mar 11 '14 at 21:51
  • 6
    Problems: you need to `git fetch` before you can `git branch` otherwise you get `fatal: Not a valid object name`. And when I got to the `git subtree split` I wasn't sure what to do after that because I got `fatal: ambiguous argument 'temp_branch': unknown revision or path not in the working tree.`. – 2rs2ts Aug 08 '14 at 20:37
  • 2
    There was a typo in the command. I edited it. A -b was needed before the name of the branch. It now works exactly as the OP intended. – Lefteris May 06 '15 at 15:12
  • 7
    then how to update the only one file or directory? do every steps again? – ChenYilong Jan 06 '16 at 08:43
  • 4
    As far I can tell, `git subtree split --squash --prefix=path_of_interest_in_project -b temp_branch` only if `path_of_interest_in_project` is a directory. In which case it grabs the commits related to all the files in that directory. Is there a way to specify one and only one __file__? – rudolph9 Aug 09 '18 at 17:38
  • 2
    This answer must be out of date and should be updated. `git subtree merge` without `git subtree add` produces a "does not exist" error for me. The `--allow-unrelated-histories` flag is not accepted either. – fuzzyTew Apr 02 '21 at 21:24
  • 1
    also `git subtree split --squash` gives "The '--squash' flag does not make sense with `git subtree split`" – stimulate Nov 11 '21 at 18:15
  • 2
    Omitting `--squash` and using `git subtree add` instead of `merge --allow-unrelated-histories` worked fine in my case! :) – stimulate Nov 11 '21 at 18:23
9

Suppose you wanted to add the images/ prefix of the octocat git repo at the ref master.

Suppose we want to use the remote hosted at https://github.com/octocat/octocat.github.io.git (Note: GitHub returns error: Server does not allow request for unadvertised object in the following fetch command is you specify a sha1 not associated with named ref)

Starting on the branch you wish to add the subtree to, let's first fetch the desired commit history and checkout our new branch

git fetch https://github.com/octocat/octocat.github.io.git master:tmp_octocat_master
git checkout tmp_octocat_master

Next, let's create a new history where only the files under our desired prefix (images/) exist.

git subtree split --prefix=images -b subtree_split_branch

Finally, let's add this subtree to our desired branch (presumably the last branch you were on, git checkout -)

git checkout -
git subtree add --prefix=public/images subtree_split_branch

Now you should have all the desired files on your current branch with a full git history.

Alt Squashed history

At time you want the commits in our subtree to be squashed into a single commit. This to a certain extent defeats the purposes of adding a subtree but it has it's place. The following is a variation of what is described above to limit the history pulled into your repo

Starting on the branch you wish to add the subtree to:

git fetch --depth=1 https://github.com/octocat/octocat.github.io.git master:tmp_octocat_master
git checkout tmp_octocat_master

Note: we are specifying --depth=1 in the above since we are using the --squash is the following git subtree split command.

git subtree split --squash --prefix=images -b subtree_split_branch
git checkout -
git subtree add --prefix=public/images subtree_split_branch
Community
  • 1
  • 1
rudolph9
  • 8,021
  • 9
  • 50
  • 80
  • NOTE: I do not believe `git subtree split` currently supports specifying a single file. It builds off and passed the `--prefix` command directly to `git read tree`. > --prefix=/ Keep the current index contents, and read the contents of the named tree-ish under the directory at . The command will refuse to overwrite entries that already existed in the original index file. Note that the / value must end with a slash. – rudolph9 Aug 09 '18 at 20:47
  • why do we need `public` in `--prefix=public/images`? – Jan33 Jan 08 '21 at 08:24
  • 2
    Then how can this subtree get updated based on its own remote? – mhr Jan 10 '21 at 14:14
4

It's a great answer from @eddiemoya, however some commands don't work as of today. So I'll just repost them here:

# create a target branch in the new repo
git branch target

# check out the other repo into a new branch
git remote add temp git@github.com:aRepo/any-service.git
git fetch temp
git branch temp_master temp/master

# split desired folder into a temp branch
git checkout -f temp_master
git subtree split --prefix=path/to/desired/folder -b temp_branch

# check out target branch and add all commits in temp branch to the correct folder
git checkout -f target
git subtree add --prefix=path/to/destination/folder temp_branch
# ... voila

# clean up
git branch -d temp_branch
git branch -d temp_master
git remote remove temp
spottedmahn
  • 14,823
  • 13
  • 108
  • 178
maximus
  • 1,290
  • 1
  • 14
  • 18
  • It's working for pulling the first time. What about updating the destination folder later? I've tried `git subtree merge` but getting `refusing to merge unrelated histories` – Wahid Bitar Nov 10 '22 at 05:29