简体   繁体   中英

Create a git repository from commits of a previous one

I was part of a project that involved creating a redmine plugin with lot of features.

Now I want to take some of the features and turn them into individual plugins. For that I will create one separate git repository for each.

In that git repository I would like to have the intact history from the original git repository with only the files related to that feature.

Should I had the older repository as a remote and go through every commit manually, finding the ones that interest me, and committing them to the new repository?

Or is there another (more practical) way to do it?

The workflow on the old git repository was:

  • There was only the master branch;

  • Every time a milestone was completed it was committed and pushed to the remote;

Look at git-filter-branch(1) which is Git's Swiss-army knife for automated rewriting of history and it certainly allows you to keep all commits touching a defined set of files (and drop everything else).

How to use it in detail, depends on your actual repository. If, for example, all the files you want to keep are already neatly contained in a subdirectory (say, foobar ), then the following would be sufficient:

git filter-branch --subdirectory-filter foobar --prune-empty -- --all

As an alternative, you could use the following to remove all files but the ones you want to keep from your repository:

git filter-branch --tree-filter 'rm -f filename1 pattern2.*' --prune-empty -- --all

Rather than rewriting history to extract just the files you're interested in, you could keep the original history — which provides the context for all the changes — and add a new commit which just deletes all the files you aren't interested in.

Clone the original repository, then make a branch for each plugin, and on each branch, delete anything that isn't relevant to that plugin. That way, all your plugins have history that traces back to their common origins.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM