Warning: this post is old and might not reflect the current state of the art
Suppose in your project you have a folder that you want to manage on a separate Git repository.
Or, suppose you want to open-source part of your application, but you don’t want the burden of managing a separate folder, symlinks and separate Git pushes.
What can you do?
First, create a new repository that will host the files you need. If using GitHub, initialize it with a README.md file so it will automatically create the
Next, add it as a remote inside your project.
$ git remote add repository-name firstname.lastname@example.org:yourname/repository-name.git $ git subtree add --prefix=subfolder/path/you/want repository-name master
yourname with your chosen names.
You can add
--squash to the last command if the project want to incorporate has already many commits, and you want to pull all changes in one single commit.
Pushing and pulling
Now, suppose someone pushes a PR on that subrepository, or you edit it from another project.
To incorporate the changes inside your project, do:
git subtree pull --prefix=subfolder/path/you/want repository-name master
You can add
--squash if you want to incorporate all changes in one single commit.
Now you’ll push to your own project, and the changes made in the subrepository are stored in the Git history of it.
To also push those changes to the subrepository Git history, and to the remote, just do
git subtree push --prefix=subfolder/path/you/want repository-name master
--squash if you feel it appropriate. Git will cherry-pick the commits that involve the subrepository subdirectory, so you’ll just see those changes in the subrepository history.
You might want to be careful and differentiate the commits that involve the subrepository. In this way you’ll keep things separated.
More git tutorials:
- A Git Cheat Sheet
- Git workflow to manage work on multiple branches
- An easy way to handle Git subrepositories
- An incomplete list of great Git tutorials
- A developer's introduction to GitHub
- The complete Git guide
- How to discover a bug using git bisect
- How to make your first Pull Request on GitHub
- How to update a Git branch from another branch
- I posted my password / API key on GitHub
- Squashing Git commits
- How to remove a Git remote