0Pricing
Git Advanced: Monorepo, Submodules & Workflows · レッスン

Subtreeの追加とマージ

外部リポジトリをsubtreeとして追加するコマンドと、upstreamから更新を取り込む方法を学びます。

「Subtreeの追加とマージ」はCoddyKit上の無料Git Advanced: Monorepo, Submodules & Workflowsレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはGit Advanced: Monorepo, Submodules & Workflows学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Git Advanced: Monorepo, Submodules & Workflowsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Subtree: Integrating External Code

Welcome! In this lesson, we'll dive into Git Subtree, a powerful way to integrate another Git repository directly into your project.

Unlike submodules, subtree merges the external repository's history into your own, making it feel like a native part of your project.

Why Use `git subtree add`?

Subtree offers a simpler workflow than submodules for many cases. Here are a few benefits:

  • No special commands: Once added, you work with subtree content like regular files.
  • Single repository: No separate .git directories or complex submodule initializations.
  • Easier for consumers: Others cloning your project don't need to learn special submodule commands.

Preparing Your Repository

Before adding a subtree, ensure your main repository is in a clean state (no uncommitted changes). You'll need:

  • The URL of the external repository.
  • The branch or commit you want to integrate (e.g., main or master).
  • A chosen path within your main repository for the subtree.

The `git subtree add` Command

To add an external repository as a subtree, you use the git subtree add command. It looks like this:

git subtree add --prefix=<path> <repository> <ref>

  • --prefix=<path>: The directory in your project where the subtree will reside.
  • <repository>: The URL of the external Git repository.
  • <ref>: The branch (e.g., main) or commit hash from the external repo.

Adding an Example Subtree

Let's say you want to add a shared utility-library from a separate Git repository into a src/shared folder in your current project. You would use:

git subtree add --prefix=src/shared/utility-library https://github.com/myorg/utility-library.git main

This command fetches the utility-library's main branch and integrates its history into your src/shared/utility-library directory.

Using the `--squash` Option

When adding a subtree, you'll often see the --squash option:

git subtree add --prefix=path repo_url main --squash

The --squash flag combines all commits from the external repository's history into a single commit in your main project. This keeps your main project's history cleaner, but you lose the individual commit details from the subtree's original history.

Why Update a Subtree?

After you've added a subtree, the original external repository might receive new updates. To keep your integrated code current with the upstream source, you'll need to pull those changes.

This ensures your project always has the latest versions of the shared components or libraries you've included as a subtree.

The `git subtree pull` Command

To pull updates from an upstream subtree, you use a similar command structure:

git subtree pull --prefix=<path> <repository> <ref>

Notice it uses the same --prefix, <repository>, and <ref> as when you added it. Git uses these to identify the correct upstream source for your subtree.

Updating the Example Subtree

Let's continue with our utility-library example. To pull the latest changes from its main branch into your project, you'd run:

git subtree pull --prefix=src/shared/utility-library https://github.com/myorg/utility-library.git main

This command fetches and merges any new commits from the utility-library's main branch into your local src/shared/utility-library directory.

Subtree Command Check

Which command is used to initially incorporate an external repository as a subtree into your project, creating a new directory for it?

Recap: Adding & Merging Subtrees

You've learned how to integrate external repositories using Git Subtree!

  • Use git subtree add with --prefix, repository URL, and branch to bring external code into your project.
  • The --squash option helps keep your main project's history clean.
  • Use git subtree pull with the same parameters to fetch and merge upstream updates into your subtree.

Subtree provides a powerful, integrated alternative to submodules for managing shared code!

よくある質問

「Subtreeの追加とマージ」レッスンは無料ですか?

はい。「Subtreeの追加とマージ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Git Advanced: Monorepo, Submodules & Workflowsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Git Advanced: Monorepo, Submodules & Workflowsコースには全4レッスンが含まれています。

「Subtreeの追加とマージ」で何を学びますか?

外部リポジトリをsubtreeとして追加するコマンドと、upstreamから更新を取り込む方法を学びます。 ブラウザで直接実行するハンズオンコードでGit Advanced: Monorepo, Submodules & Workflowsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Git Advanced: Monorepo, Submodules & Workflowsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのGit Advanced: Monorepo, Submodules & Workflowsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「Subtreeの追加とマージ」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このGit Advanced: Monorepo, Submodules & Workflowsレッスンでコードを書いて実行できますか?

はい。すべてのGit Advanced: Monorepo, Submodules & Workflowsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Git Subtree入門
  2. Subtreeの追加とマージ
  3. SubtreeとSubmoduleの比較
  4. Subtree Splitによる変更のアップストリームへの反映
← Git Advanced: Monorepo, Submodules & Workflowsに戻る