How to extract sub-items from an existing Git repository as a separate repository and keep its commit history

Source: Internet
Author: User
Tags git clone

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.