我想从具有不同目录结构的不同存储库中选择一些文件和更改,而不是复制内容并保留历史记录。在git中可以吗?
最佳答案
如“Moving Files from one Git Repository to Another, Preserving History ”中所述,如果您要导入的文件位于文件夹中,这会更容易:导出/导入文件夹(及其历史记录)比仅导出/导入文件更容易。
Goal: Move directory 1 from Git repository A to Git repository B.
- Make a copy of repository A so you can mess with it without worrying about mistakes too much.
- It’s also a good idea to delete the link to the original repository to avoid accidentally making any remote changes (line 3).
- Line 4 is the critical step here. It goes through your history and files, removing anything that is not in directory 1
即:
git clone <git repository A url>
cd <git repository A directory>
git remote rm origin
git filter-branch --subdirectory-filter <directory 1> -- --all
mkdir <directory 1>
mv * <directory 1>
git add .
git commit
- Make a copy of repository B if you don’t have one already.
- On line 3, you’ll create a remote connection to repository A as a branch in repository B.
- Then simply pull from this branch (containing only the directory you want to move) into repository B.
- The pull copies both files and history.
第 2 步:
git clone <git repository B url>
cd <git repository B directory>
git remote add repo-A-branch <git repository A directory>
git pull repo-A-branch master --allow-unrelated-histories
git Remote rm repo-A-branch
您还有 this answer 中描述的其他选项或this one ,它保留完整路径名(但强制您指定要排除的所有路径,这比指定要保留的一个文件夹要长)。
# 1. clone the source
git clone ssh://<user>@<source-repo url>
cd <source-repo>
# 2. remove the stuff we want to exclude
git filter-branch --tree-filter "rm -rf <files to exclude>" --prune-empty HEAD
# 3. move to target repo and create a merge branch (for safety)
cd <path to target-repo>
git checkout -b <merge branch>
# 4. Add the source-repo as remote
git remote add source-repo <path to source-repo>
# 5. fetch it
git pull source-repo master
关于GIT:是否可以从具有不同目录结构的不同存储库中选取文件或更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34235940/