Most of the time, we will encounter a lot of small projects in a git repository, but as some projects become more demanding or market demand, we need to pull them out and be maintained and developed as a separate project.
However, if the direct copy file is pasted into the new Git repository, the original commit will be discarded, so we cannot do so rudely.
What do we do? The following will be explained in detail according to an example.
If we include the following small items in the Demos warehouse, where we want to separate the files under the Comet directory as a separate project maintenance development, as follows:
First, we use Git clone to import demos into the local, and then through GIT remote RM origin, cut off the connection to the remote repository, as follows:
Then, run git filter-branch--tag-name-filter cat--prune-empty--subdirectory-filter <name-of-folder> commands, Filters out and retains a commit to the specified subdirectory and sets the subdirectory to the root of the repository:
1.--tag-name-filter: Control how we handle the old Tag,cat expression as output
2.--prune-empty: Delete empty commits, which are not affected by subdirectories
3.--subdirectory-filter: Specify the subdirectory path
After running the command, we can see that our original warehouse has changed to this:
Although on the surface, we have reached our goal, the new warehouse has become a subdirectory of the content, and also retained the relevant commit, but the new warehouse of the. Git directory is not reset, so we also need to run the following command to thin the. git, as follows:
Well, so far, the subproject extraction work has been completed.
Finally, the local sub-project is push to the remote repository. Since we have broken the original remote repository link before, we need to link this subproject to the new remote repository at this point.
Suppose, we create a new remote repository named Testrepo, as follows:
As prompted, we link and push the subproject to the remote repository via the GIT remote add command, as follows:
This completes our goal, the following is the remote warehouse Testrepo results: