You can add rock as a submodule of slingshot. In the slingshot repository:. Newer versions of Git will do this automatically, but older versions will require you to explicitly tell Git to download the contents of rock :. On GitHub, the rock folder icon will have a little indicator showing that it is a submodule:.
And clicking on the rock folder will take you over to the rock repository. You can interact with all the content from rock as if it were a folder inside slingshot because it is. It can be a little tricky to take an existing subfolder and turn it into an external dependency. You can use git filter-branch to do this, leaving you with just the commits related to rubber-band. For more information on git filter-branch , see this article.
The first step is to make a copy of slingshot to work on—the end-goal is for rubber-band to stand as its own repository, so leave slingshot as is. You can use cp with -r to recursively copy the entire slingshot folder to a new folder rubber-band.
It looks like rubber-band is just another slingshot , but now, from the rubber-band repository, run git filter-branch :. We got an actual merge conflict here, so if we resolve that and commit it, then we can simply update the main project with the result. Interestingly, there is another case that Git handles. If a merge commit exists in the submodule directory that contains both commits in its history, Git will suggest it to you as a possible solution.
The suggested command Git is providing will update the index as though you had run git add which clears the conflict , then commit.
You can just as easily go into the submodule directory, see what the difference is, fast-forward to this commit, test it properly, and then commit it. There is a foreach submodule command to run some arbitrary command in each submodule. This can be really helpful if you have a number of submodules in the same project. We can easily stash all the work in all our submodules.
You get the idea. One really useful thing you can do is produce a nice unified diff of what is changed in your main project and all your subprojects as well. This is obviously a simplified example, but hopefully it gives you an idea of how this may be useful. We covered setting up Git aliases in Git Aliases , but here is an example of what you may want to set up if you plan on working with submodules in Git a lot.
This way you can simply run git supdate when you want to update your submodules, or git spush to push with submodule dependency checking. For instance, switching branches with submodules in them can also be tricky with Git versions older than Git 2.
If you create a new branch, add a submodule there, and then switch back to a branch without that submodule, you still have the submodule directory as an untracked directory:.
If you do remove it and then switch back to the branch that has that submodule, you will need to run submodule update --init to repopulate it. Using the --recurse-submodules flag of git checkout can also be useful when you work on several branches in the superproject, each having your submodule pointing at different commits.
That is because the submodule state is by default not carried over when switching branches. For older Git versions that do not have the --recurse-submodules flag, after the checkout you can use git submodule update --init --recursive to put the submodules in the right state. As noted above, this will also make Git recurse into submodules for every command that has a --recurse-submodules option except git clone. The other main caveat that many people run into involves switching from subdirectories to submodules.
Assume that you have files in a subdirectory of your project, and you want to switch it to a submodule. If you delete the subdirectory and then run submodule add , Git yells at you:. You have to unstage the CryptoLibrary directory first.
Then you can add the submodule:. Now suppose you did that in a branch. If you try to switch back to a branch where those files are still in the actual tree rather than a submodule — you get this error:.
Then, when you switch back, you get an empty CryptoLibrary directory for some reason and git submodule update may not fix it either. You may need to go into your submodule directory and run a git checkout.
You could run this in a submodule foreach script to run it for multiple submodules. With these tools, submodules can be a fairly simple and effective method for developing on several related but still separate projects simultaneously.
Getting Started 1. Git Basics 2. Git Branching 3. Git on the Server 4. Distributed Git 5. GitHub 6. Git Tools 7. Customizing Git 8. Git and Other Systems 9. Git Internals Appendix C: Git Commands A3. Checking connectivity Submodule path 'DbConnector': checked out 'c3f01dcdddb05bc7b29bc'. Working on a Project with Submodules Now we have a copy of a project with submodules in it and will collaborate with our teammates on both the main project and the submodule project.
Pulling in Upstream Changes from the Submodule Remote The simplest model of using submodules in a project would be if you were simply consuming a subproject and wanted to get updates from it from time to time but were not actually modifying anything in your checkout.
Applying: Unicode support Submodule path 'DbConnector': rebased into '5d60ef9bbebf5a0c1cfceeb54ad58da94'. Aborting Unable to checkout 'c75e92a2bc9e5b66fd9dbaca' in submodule path 'DbConnector'. Unable to merge 'c75e92a2bc9e5b66fd9dbaca' in submodule path 'DbConnector'. You can go into the submodule directory and fix the conflict just as you normally would. Publishing Submodule Changes Now we have some changes in our submodule directory.
Git submodules are simply a reference to another repository at a particular snapshot in time. Git submodules enable a Git repository to incorporate and track version history of external code.
Often a code repository will depend upon external code. This external code can be incorporated in a few different ways. The external code can be directly copied and pasted into the main repository.
This method has the downside of losing any upstream changes to the external repository. Another method of incorporating external code is through the use of a language's package management system like Ruby Gems or NPM.
This method has the downside of requiring installation and version management at all places the origin code is deployed. Both of these suggested incorporation methods do not enable tracking edits and changes to the external repository. A git submodule is a record within a host git repository that points to a specific commit in another external repository.
Submodules are very static and only track specific commits. Submodules do not track git refs or branches and are not automatically updated when the host repository is updated. When adding a submodule to a repository a new. If the host repository has multiple submodules, the. If you need to maintain a strict version management over your external dependencies, it can make sense to use git submodules.
The following are a few best use cases for git submodules. The git submodule add is used to add a new submodule to an existing repository. The following is an example that creates an empty repo and explores git submodules. This sequence of commands will create a new directory git-submodule-demo , enter that directory, and initialize it as a new repository. Next we will add a submodule to this fresh new repo. The git submodule add command takes a URL parameter that points to a git repository. Here we have added the awesomelibrary as a submodule.
Git will immediately clone the submodule. We can now review the current state of the repository using git status There are now two new files in the repository. Looking at the contents of. The default behavior of git submodule init is to copy the mapping from the.
This may seem redundant and lead to questioning git submodule init usefulness. This enables a workflow of activating only specific submodules that are needed for work on the repository. This can be helpful if there are many submodules in a repo but they don't all need to be fetched for work you are doing. Once submodules are properly initialized and updated within a parent repository they can be utilized exactly like stand-alone repositories.
0コメント