How to Resolve Git Submodule Conflicts
If you already have a conflict and want the commands, start with Resolution steps.
Why do submodule conflicts happen?
Section titled “Why do submodule conflicts happen?”A parent repository records the commit a submodule points to, rather than tracking its files directly. When branches update that reference independently, Git examines the ancestry of the submodule commits. If one is a descendant of the other, Git can fast-forward to the descendant. Otherwise, the reference needs to be resolved in the parent repository. See Merging Submodule Changes in Pro Git.
flowchart LR
A[main: docs → A] --> C[Merge]
B[feature: docs → B] --> C
C --> D{Same reference or<br/>ancestor relationship?}
D -->|Yes| E[Can resolve automatically]
D -->|No| F[Resolve reference]
Example: two branches from the same parent commit
Section titled “Example: two branches from the same parent commit”Consider a parent repository and its docs/ submodule:
- The parent’s
mainbranch is at commitP0. docs/points to submodule commitS0.- Both
feature-aandfeature-bare created fromP0.
flowchart LR
P0[main: docs → S0] --> FA[feature-a: docs → S0]
P0 --> FB[feature-b: docs → S0]
Creating branches alone does not change the reference or cause a conflict.
How Git decides
Section titled “How Git decides”The parent does not merge the submodule’s files directly. It handles the commit references, called gitlinks, recorded on each side. When both sides point to different commits, Git can inspect the submodule history and select the descendant if there is an ancestor relationship. Fetch the relevant commits and history first if they are not available locally.
| Situation | Result |
|---|---|
| Only one branch changes the submodule reference | The changed reference is normally selected without conflict |
| Both branches change it to the same SHA | No reference conflict |
| Both change it to different SHAs, one an ancestor of the other | Git can fast-forward to the descendant |
| Both change it to different SHAs without an ancestor relationship | The reference must be resolved |
| Neither branch changes it | No reference conflict |
Case 1: only one branch updates the submodule
Section titled “Case 1: only one branch updates the submodule”feature-a updates docs/ to S1 and commits that reference in the parent. feature-b changes only another parent file and leaves the reference at S0.
flowchart LR
P0[Base: docs → S0] --> P1[feature-a: docs → S1]
P0 --> P2[feature-b: docs → S0]
P1 & P2 --> M[Merge] --> OK[No conflict: S1]
When the branches are merged into main, the updated reference is normally selected without a conflict.
Case 2: both branches select the same SHA
Section titled “Case 2: both branches select the same SHA”Both branches point the submodule to the same existing commit S1 and commit that reference in the parent.
flowchart LR
P0[Base: docs → S0] --> P1[feature-a: docs → S1]
P0 --> P2[feature-b: docs → S1]
P1 & P2 --> M[Merge] --> OK[No conflict]
Because both parent branches record S1, their references do not conflict.
Case 3: both branches select diverged commits
Section titled “Case 3: both branches select diverged commits”Suppose feature-a records S1 and feature-b records S2, and both submodule commits diverged from S0: neither is an ancestor of the other. Git cannot fast-forward one reference to the other, even if their changes look similar.
flowchart LR
P0[Base: docs → S0] --> P1[feature-a: docs → S1]
P0 --> P2[feature-b: docs → S2]
P1 & P2 --> M[Merge] --> NG[Resolve: S1 or S2?]
The parent may show both modified: docs; someone must decide which commit the parent should record. If S1 were an ancestor of S2, Git could instead select S2 automatically.
Case 4: identical content, different commits
Section titled “Case 4: identical content, different commits”Suppose the same typo fix is committed separately from S0 in each branch. The resulting abc1234 and def5678 commits have no ancestor relationship, even if their file contents match.
flowchart LR
P0[Parent: docs → S0] --> PA[feature-a: docs → abc1234]
P0 --> PB[feature-b: docs → def5678]
PA & PB --> C[Resolve two references]
Even if comparing the submodule contents produces no diff, Git cannot fast-forward between these two unrelated gitlinks. The reference still needs resolution.
Case 5: only parent files change
Section titled “Case 5: only parent files change”If both branches change parent files such as README or astro.config.mjs without changing the submodule reference, it remains at S0. There is no submodule reference conflict; ordinary file merges still apply.
Case 6: merging branches in sequence
Section titled “Case 6: merging branches in sequence”After merging feature-a into main, rebasing feature-b onto the updated main before merging can avoid a conflict in some cases. If feature-b also changes the submodule reference to a different SHA, a conflict can appear during the rebase when the references cannot be fast-forwarded.
sequenceDiagram
participant M as main
participant A as feature-a
participant B as feature-b
M->>A: Branch
M->>B: Branch
A->>M: Merge docs → S1
B->>M: Incorporate main
Note over B,M: Diverged references may conflict
Resolution approach
Section titled “Resolution approach”- Identify the conflicted path in the parent repository.
- Inspect the submodule commits and decide which commit, or a new commit containing the desired changes, to use.
- Record that single reference in the parent repository and complete the merge or rebase.
Resolution steps
Section titled “Resolution steps”1. Check the conflict in the parent repository
Section titled “1. Check the conflict in the parent repository”git statusA submodule reference conflict can appear as both modified: path/to/submodule.
2. Choose a commit in the submodule
Section titled “2. Choose a commit in the submodule”cd path/to/submodulegit log --oneline --decorate --graph -n 20Inspect the commits from both sides. If necessary, merge the changes in the submodule to create a commit containing both. When choosing an existing commit, check out the intended commit and inspect it. Protect any uncommitted changes first.
git checkout <chosen-commit-sha>git statusThis checkout leaves the submodule in detached HEAD state. If you created a new commit, save it on a branch and make it available to others before sharing the parent reference.
3. Record the reference in the parent repository
Section titled “3. Record the reference in the parent repository”cd /path/to/parent-repositorygit add path/to/submodulegit commit -m "Resolve submodule pointer conflict"Replace the parent path with your actual repository root. git add stages the selected submodule SHA in the parent; it does not stage the submodule’s individual files. If you are resolving a rebase, follow the instructions in git status to continue it instead of creating the merge commit shown above.
Common misconceptions
Section titled “Common misconceptions”Does identical content prevent a conflict?
Section titled “Does identical content prevent a conflict?”Identical content alone does not guarantee automatic resolution. Two separately created commits with no ancestor relationship can have identical files but still require reference resolution, as in case 4. If one commit is an ancestor of the other, Git can fast-forward instead.
Does a clean submodule merge guarantee a clean parent merge?
Section titled “Does a clean submodule merge guarantee a clean parent merge?”Not necessarily. The parent can still encounter conflicting references if the commits recorded by its branches have no ancestor relationship. Resolve the parent reference to one commit and inspect that commit’s contents in the submodule as needed.
Reducing future conflicts
Section titled “Reducing future conflicts”- Keep pull requests that update submodule references focused.
- Before merging, incorporate the relevant latest changes in both repositories.
- Separate submodule updates from unrelated features when that makes review easier.
- Explain in the pull request which submodule commit is selected and why.