この投稿では、Gitサブリポジトリを管理するために見つけた最も簡単な方法について説明します。
警告:この投稿は古く、現在の最先端技術を反映していない可能性があります
プロジェクトに、別のGitリポジトリで管理するフォルダーがあるとします。
または、アプリケーションの一部をオープンソースにしたいが、個別のフォルダー、シンボリックリンク、および個別のGitプッシュを管理する負担を望まないとします。
あなたは何ができますか?
サブツリーを入力してください
まず、必要なファイルをホストする新しいリポジトリを作成します。 GitHubを使用している場合は、README.mdファイルで初期化して、自動的にmaster
ブランチ。
次に、プロジェクト内にリモートとして追加します。
$ git remote add repository-name [email protected]:yourname/repository-name.git
$ git subtree add --prefix=subfolder/path/you/want repository-name masterChange repository-name
, subfolder/path/you/want
and 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.
You’re done!
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 masterYou 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 masterAgain, use --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