GIT:是否可以从不同目录结构的不同目录中选择文件或更改 [英] GIT: Is it possible to pick files or changes from a different repo with different directory structure
问题描述
我想从一个不同的目录结构中选择一些文件和更改,而不是复制内容并保留历史记录。在git中可能吗?
正如,如果将文件从一个Git存储库移动到另一个,保留历史记录您想要导入的文件位于文件夹中:导出/导入文件夹(使用其历史记录)比文件更简单。
目标:将目录1从Git存储库A移动到Git存储库B.
- 制作一个存储库A的副本,不用担心错误太多。
- 删除指向原始存储库的链接也是一个好主意,以避免意外做出任何远程更改(第3行)。
- 第4行是这里的关键步骤。它通过你的历史和文件,删除不在目录1中的任何东西。
即:
git clone< git repository A url>
cd< git repository目录>
git remote rm origin
git filter-branch --subdirectory-filter< directory 1> - - all
mkdir< directory 1>
mv *< directory 1>
git add。
git commit
blockquote>
- 制作如果您还没有存储库B的副本。
- 在第3行中,您将创建一个到存储库A的远程连接,作为存储库B中的一个分支。
然后,只需从该分支只包含您想要移动的目录)。
- 拉取文件和历史记录。
第2步:
git clone< git repository B url>
cd< git repository B目录>
git remote add repo -A-branch< git repository目录>
git pull repo -A-branch master
git remote rm repo -A-branch
您可以在此答案或这一个,它保留了完整的路径名(但是强制你指定你想要排除的所有内容,这比指定一个长
#1.克隆源代码
git clone ssh://< user> ; @< source-repo url>
cd< source-repo>
#2.删除我们想要排除的内容
git filter-branch --tree-filterrm -rf<要排除的文件> --prune-empty HEAD
#3.移动到目标repo并创建合并分支(为了安全)
cd<目标回购路径>
git checkout -b< merge branch>
#4.将source-repo添加为远程
git remote add source-repo< path to source-repo>
#5.抓取它
git pull source-repo master
I want to pick some files and changes from a different repo which has different directory structure rather than copy things and to keep history. Is it possible in git?
解决方案As described in "Moving Files from one Git Repository to Another, Preserving History", this is easier if the file(s) you want to import are in a folder: exporting/importing a folder (with its history) is easier than just a file.
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
That is:
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.
Step 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
git remote rm repo-A-branch
You have other options described in this answer or this one, which preserves the full path name (but forces you to specify all that you want to exclude, which is longer than specifying the one folder you want to keep).
# 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:是否可以从不同目录结构的不同目录中选择文件或更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!