Questions tagged [git-subtree]

An alternative to the git-submodule command. Merges and splits subtrees from your project into subprojects and back. git-subtree has been merged into mainline git as of 1.7.11.

Subtrees allow subprojects to be included within a subdirectory of the main project, optionally including the subproject's entire history.

For example, you could include the source code for a library as a subdirectory of your application.

Subtrees are not to be confused with submodules, which are meant for the same task. Unlike submodules, subtrees do not need any special constructions (like .gitmodule files or gitlinks) be present in your repository, and do not force end-users of your repository to do anything special or to understand how subtrees work. A subtree is just a subdirectory that can be committed to, branched, and merged along with your project in any way you want.

They are also not to be confused with using the subtree merge strategy. The main difference is that, besides merging the other project as a subdirectory, you can also extract the entire history of a subdirectory from your project and make it into a standalone project. Unlike the subtree merge strategy you can alternate back and forth between these two operations. If the standalone library gets updated, you can automatically merge the changes into your project; if you update the library inside your project, you can "split" the changes back out again and merge them back into the library project.

For example, if a library you made for one application ends up being useful elsewhere, you can extract its entire history and publish that as its own git repository, without accidentally intermingling the history of your application project.

Note that git subtree is not an official Git command, it is a script in the contrib/ directory of the Git repository of the Git project. Some distributions do install it along with the rest of Git, though.

544 questions
2187
votes
26 answers

How do you merge two Git repositories?

Consider the following scenario: I have developed a small experimental project A in its own Git repo. It has now matured, and I'd like A to be part of larger project B, which has its own big repository. I'd now like to add A as a subdirectory of…
static_rtti
  • 53,760
  • 47
  • 136
  • 192
1967
votes
26 answers

Detach (move) subdirectory into separate Git repository

I have a Git repository which contains a number of subdirectories. Now I have found that one of the subdirectories is unrelated to the other and should be detached to a separate repository. How can I do this while keeping the history of the files…
matli
  • 27,922
  • 6
  • 37
  • 37
403
votes
6 answers

Differences between git submodule and subtree

What are the conceptual differences between using git submodule and subtree? What are the typical scenarios for each?
Nathan H
  • 48,033
  • 60
  • 165
  • 247
327
votes
10 answers

Merge two Git repositories without breaking file history

I need to merge two Git repositories into a brand new, third repository. I've found many descriptions of how to do this using a subtree merge (for example Jakub Narębski's answer on How do you merge two Git repositories?) and following those…
Eric Lee
  • 8,181
  • 4
  • 21
  • 18
125
votes
5 answers

When to use git subtree?

What problem does git subtree solve? When and why should I use that feature? I've read that it is used for repository separation. But why would I not just create two independent repositories instead of sticking two unrelated ones into one? This…
Lernkurve
  • 20,203
  • 28
  • 86
  • 118
102
votes
4 answers

Alternatives to Git Submodules?

I feel that using Git submodules is somehow troublesome for my development workflow. I've heard about Git subtree and Gitslave. Are there more tools out there for multiple repository projects and how do they compare ? Can these tools run on…
Chau Chee Yang
  • 18,422
  • 16
  • 68
  • 132
99
votes
12 answers

Why can't I push this up-to-date Git subtree?

I am using Git subtree with a couple of projects that I am working on, in order to share some base code between them. The base code gets updated often, and the upgrades can happen in anyone of the projects, with all of them getting updated,…
mateusz
  • 1,127
  • 1
  • 8
  • 10
80
votes
9 answers

How to rebase after git-subtree add?

I'm trying to learn the new git-subtree command which was added in Git 1.7.11. I seem to lose ability to rebase after I add a subtree. I have the primary repository with README file and a library repository which also has a README file. I add it to…
esamatti
  • 18,293
  • 11
  • 75
  • 82
64
votes
2 answers

Add subdirectory of remote repo with git-subtree

Is there a way to add a subdirectory of a remote repository into a subdirectory of my repository with git-subtree? Suppose I have this main repository: / dir1 dir2 And this library repository: / libdir some-file …
Yogu
  • 9,165
  • 5
  • 37
  • 58
56
votes
4 answers

How do I force a subtree push to overwrite remote changes?

We use a subtree deployment a lá this Gist to deploy a subdirectory of our Yeoman project. In our case, the branch is called production, not gh-pages. This worked perfectly until yesterday when the Git server rejected the command git subtree push…
ele
  • 6,021
  • 5
  • 25
  • 35
53
votes
4 answers

How can I list the git subtrees on the root?

For example, you can do a git remote --verbose and git will show all the remotes you have on your project, git branch will show all the branches and signal the current branch, but how to list all subtrees, without any destructive command? git…
pocesar
  • 6,860
  • 6
  • 56
  • 88
45
votes
3 answers

Why is the `git subtree` not working even though I have git 1.8.3.2 installed on Ubuntu 12.10?

I am about to deploy my AngularJS based static site as a Github Page with Yeoman. Step 3 of the deployment instructions tell me to do git subtree push --prefix dist origin gh-pages but when I enter that I get git: 'subtree' is not a git command.…
user883807
43
votes
3 answers

Git Subtree only one file or directory

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…
kicaj
  • 2,881
  • 5
  • 42
  • 68
42
votes
1 answer

Forking a sub directory of a repository on GitHub and making it part of my own repo

Apologies, I am very new to Git and GitHub, I've read through a few things but I'm not sure if what I'm trying to do is entirely possible. Basically I want to fork the Confluence Skin used on XBMC and modify various elements located…
James White
  • 676
  • 1
  • 10
  • 18
32
votes
4 answers

git-subtree pull complications

We have been trying to get git-subtree working on a project (with git version 1.7.9.4) and have run into a bit of a complication. Someone else previous added the subtree with this command some months ago: git subtree add --prefix=foo…
David H. Clements
  • 3,590
  • 2
  • 24
  • 26
1
2 3
36 37