Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Moving files into an existing submodule

How do I move files and folders in my master repo into an existing submodule? Can I preserve history somehow?

like image 461
MKaras Avatar asked Aug 22 '14 20:08

MKaras


People also ask

How do I add an existing git repository to a submodule?

In order to add a Git submodule, use the “git submodule add” command and specify the URL of the Git remote repository to be included as a submodule. When adding a Git submodule, your submodule will be staged. As a consequence, you will need to commit your submodule by using the “git commit” command.

Can you push to a submodule?

In the parent repo, you can also use git push --recurse-submodules=check which prevents pushing the parent repo if the submodule(s) are not pushed first. Another option is git push --recurse-submodules=on-demand which will try to push the submodules automatically (if necessary) before pushing the parent repo.

Why you should not use git submodules?

This is because of some major drawbacks around git submodules, such as being locked to a specific version of the outer repo, the lacking of effective merge management, and the general notion that the Git repository itself doesn't really know it's now a multi-module repository.

Is using git submodules a good idea?

Git submodules may look powerful or cool upfront, but for all the reasons above it is a bad idea to share code using submodules, especially when the code changes frequently. It will be much worse when you have more and more developers working on the same repos.

What happens when you move a submodule in Git?

Report the names of files as they are moved. Moving a submodule using a gitfile (which means they were cloned with a Git version 1.7.8 or newer) will update the gitfile and core.worktree setting to make the submodule work in the new location.

How do I add a library to a Git submodule?

Let’s start by adding an existing Git repository as a submodule of the repository that we’re working on. To add a new submodule you use the git submodule add command with the absolute or relative URL of the project you would like to start tracking. In this example, we’ll add a library called “DbConnector”.

Can I move files from one project to another in Git?

If you’ve been tracking files in your project and you want to move them out into a submodule, you must be careful or Git will get angry at you. Assume that you have files in a subdirectory of your project, and you want to switch it to a submodule.

How do I move a submodule from one repository to another?

Move the submodule to its new home. Edit the .git file in the submodule's working directory, and modify the path it contains so that it points to the right directory in the master repository's .git/modules directory. Enter the master repository's .git/modules directory, and find the directory corresponding to your submodule.


1 Answers

Assuming you have only a single branch in you repos: take your submodule repo, add your master repo as a second remote, fetch the history and remove everything but the chosen files. merge the result to you master repository branch.

Here "remove" could mean two things. Either a simple git rm which will create a clean HEAD but the deleted files will remain in history. Or a git filter-branch which is able to create a new history consisting only of the chosen files.

At your master repository you have basically the same options. Either use git rm to delete the moved files in HEAD or use git filter-branch to remove them from the whole history.

like image 97
michas Avatar answered Oct 13 '22 03:10

michas