简体   繁体   中英

Git repository based on subfolder of another repo

I have a repo which contains a subdirectory.

I want to turn that subdirectory into a spin off git repo, and then add my own commits onto it.

I also want to be able to merge in changes from the original parent repo as and when they happen, but naturally only the commits which affect files in the subdirectory.

Is this a thing that can be done?

This is exactly the situation the git subtree command was designed for. (Note that this is different from the "subtree merge strategy"). You can find out more about git subtree here:

The short answer: This is nontrivial, and I question whether it's worth your while. I'd consider simply forking the entire repository and only making changes to the subdirectory.

That said, if you really need the subdirectory on its own, two basic options come to mind:

  • Simply copy the files from the subdirectory into a fresh repository and remove them from the old repository. You'll lose history, but it's easy.

  • Create a new repository, add the old repository as a remote, and cherry-pick the commits from the old repository that modify the directory you're interested in. This keeps history in the new repository, but it gives you a repository that contains a single directory, which you may not want.

Merging changes from the old repository is possible with the second strategy; you can simply cherry-pick commits from the old repository as you see fit.

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