Data report"State of code review 2024" is now liveRead the full report

How to solve submodule conflicts in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Submodules in Git allow developers to keep a Git repository as a directory in another Git repository. This is useful for including libraries, frameworks, or other projects where you need to maintain a specific version within your project. However, managing submodules can lead to conflicts, especially when multiple branches or forks are involved. Here's a detailed guide on how to solve submodule conflicts in Git.

Submodule conflicts typically occur when different branches have different commits in the submodule directory, and these branches are merged. Since the submodule is linked to a specific commit, any divergence needs to be resolved manually.

  1. Identify the conflict: First, you need to understand where the conflict is coming from. Run git status to see which submodule is in conflict. You might see output similar to this:

    Terminal
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.
    Changes to be committed:
    (use "git restore --staged <file>..." to unstage)
    modified: path/to/submodule (new commits)
    Unmerged paths:
    (use "git add <file>..." to mark resolution)
    both modified: path/to/submodule
  2. Navigate to the submodule directory: Change to the submodule directory and fetch the latest changes. This ensures you have all the necessary data to make an informed decision about which commit to choose.

    Terminal
    $ cd path/to/submodule
    $ git fetch
  3. Check the logs: Use git log or a graphical tool to examine the commits that are causing the conflict. This will help you decide which commit is more appropriate for your project needs.

    Terminal
    $ git log --oneline --graph
  4. Choose the correct commit: Decide on the commit that should be active in the submodule. This decision should be based on the needs of your main project and the compatibility with other parts of your codebase.

    Terminal
    $ git checkout <desired-commit-sha>
  5. Update the main project: Once you've selected the appropriate commit in the submodule, navigate back to the main project directory and add the submodule changes.

    Terminal
    $ cd ..
    $ git add path/to/submodule
    $ git commit -m "Resolve submodule conflict by selecting <commit-sha>"
  6. Test the changes: Before pushing your changes, test your project thoroughly to ensure that the submodule is working correctly with your main project. Run any tests or builds necessary to confirm everything integrates well.

  7. Push the changes: After testing, push your changes to the remote repository. This will ensure that other contributors can access the resolved state of your project.

    Terminal
    $ git push origin main
  • Regularly sync your submodules: To avoid conflicts, regularly update and synchronize your submodules with upstream changes. This can be done using git submodule update --remote.
  • Communicate changes: If you work in a team, communicate any changes to submodules to your team members. This helps everyone stay aligned and reduces the risk of conflicts.
  • Document submodule dependencies: Clearly document the reasons for pinning to specific submodule commits. This documentation will be invaluable for future debugging and development.

For further reading on Git submodules, see the official Git documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2