Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Mastering Git

You're reading from   Mastering Git Attain expert-level proficiency with Git by mastering distributed version control features

Arrow left icon
Product type Paperback
Published in Aug 2024
Publisher Packt
ISBN-13 9781835086070
Length 444 pages
Edition 2nd Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Jakub Narębski Jakub Narębski
Author Profile Icon Jakub Narębski
Jakub Narębski
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Part 1 - Exploring Project History and Managing Your Own Work FREE CHAPTER
2. Chapter 1: Git Basics in Practice 3. Chapter 2: Developing with Git 4. Chapter 3: Managing Your Worktrees 5. Chapter 4: Exploring Project History 6. Chapter 5: Searching Through the Repository 7. Part 2 - Working with Other Developers
8. Chapter 6: Collaborative Development with Git 9. Chapter 7: Publishing Your Changes 10. Chapter 8: Advanced Branching Techniques 11. Chapter 9: Merging Changes Together 12. Chapter 10: Keeping History Clean 13. Part 3 - Managing, Configuring, and Extending Git
14. Chapter 11: Managing Subprojects 15. Chapter 12: Managing Large Repositories 16. Chapter 13: Customizing and Extending Git 17. Chapter 14: Git Administration 18. Chapter 15: Git Best Practices 19. Index 20. Other Books You May Enjoy

Transforming a subfolder into a subtree or submodule

The first issue that comes to mind while thinking of the use cases of subprojects in Git is about having the source code of the base project ready for such division.

Submodules and subtrees are always expressed as subdirectories of the superproject (the master project). You can’t mix files from different subsystems in one directory.

Experience shows that most systems use such a directory hierarchy, even in monolithic repositories, which is a good beginning for modularization efforts. Therefore, transforming a subfolder into a real submodule/subtree is fairly easy and can be done in the following sequence of steps:

  1. Move the subdirectory in question outside the working area of a superproject to have it beside the top directory of the superproject. If it is important to keep the history of a subproject, consider using git subtree split, or git filter-branch --subdirectory-filter or its equivalent, perhaps together with tools such as reposurgeon to clean up the history. See Chapter 10, Keeping History Clean, for more details.
  2. Rename the directory with the subproject repository to better express the essence of the extracted component. For example, a subdirectory originally named refresh could be renamed refresh-client-app-plugin.
  3. Create the public repository (upstream) for the subproject as a first-class project (for example, create a new project on GitHub to keep extracted code, either under the same organization as a superproject, or under a specialized organization for application plugins).
  4. Initialize a self-sufficient and standalone plugin as a Git repository with git init. If, in step 1, you have extracted the history of the subdirectory into some branch, then push this branch into the just-created repository. Set up the public repository created in step 3 as a default remote repository and push the initial commit (or the whole history) to the just-created URL to store the subproject code.
  5. In the superproject, read the subproject you have just extracted but, this time, as a proper submodule or subtree, whichever solution is a better fit and whichever method you prefer to use. Use the URL of the just-created public repository for the subproject.
  6. Commit the changes in the superproject and push them to its public repository, in the case of submodules, including the newly created (or the just modified) .gitmodules file.

The recommended practice for the transformation of a subdirectory into a standalone submodule is to use a read-only URL for cloning (adding back) a submodule. This means that you can use either the git:// protocol (warning: in this case, the server is unauthenticated) or https:// without a username. The goal of this recommendation is to enforce separation by moving the work on a submodule code to a standalone separate subproject repository. In order to ensure that the submodule commits are available to all other developers, every change should go through the public repository for a subproject.

If this recommendation (best practice) is met with a categorical refusal, in practice, you could work on the subproject source code directly inside the superproject, though it is more error-prone. You would need to remember to commit and push in the submodule first, doing it from inside of the nested submodule subdirectory; otherwise, other developers would be not able to get the changes. This combined approach might be simpler to use, but it loses the true separation between implementing and consuming changes, which should be better assumed while using submodules.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image