例如,假设我有存储库 1 和存储库 2。存储库 1 有一个文件 /a/b/c/d
。我可以将此文件作为 /e/f/g/h
导入到存储库 2 中吗?
原因是我想从不同 git 存储库的实验分支中提取更改。我尝试将所有内容合并在一起,但存在大量(各种)冲突。因此,我怀疑我是否可以合并整个分支,但我想尝试尽可能多地引入。
有什么方法可以做我想做的事情,或者我只能直接复制文件吗?
For instance, suppose I have Repository 1 and Repository 2. Repository 1 has a file /a/b/c/d
. Would it be possible for me to import this file into Repository 2 as /e/f/g/h
?
The reason being that I want to pull in changes from an experimental branch from a different git repository. I tried merging everything together, but there were a ton of conflicts (of every kind). Therefore, I doubt that I can merge the entire branch in, but I would like to try to bring in as much as I can.
Is there any way to do what I want to do, or am I just going to have to resort to copying files directly?
发布评论
评论(4)
您可以使用 git archive 从远程存储库获取文件。然后,您可以将文件添加并提交到您的存储库。这种方法不会保留他的分支的历史。有关详细信息,请参阅 git 存档文档。
如果您想尝试保留该实验分支的部分内容,您可以
git fetch
他的存储库,然后git rebase
他的实验分支到您的存储库,编辑或跳过提交适当的冲突。一旦您的存储库中有一个清理过的分支,您就可以将其合并。请参阅 git rebase 文档< /a>You can get files from the remote repository using
git archive
. You could then add and commit the files to your repository. This approach will not preserve history from his branch. See git archive docs for details.If you want to try to preserve parts of that experimental branch, you could
git fetch
his repository, and thengit rebase
his experimental branch onto your repository, editing or skipping commits with conflicts as appropriate. Once you have a cleaned up branch in your repository, you can merge that in. See git rebase docs没有简单的修复方法,但有一个写得很好的指南 这里(将文件从一个存储库移动到另一个存储库,保留 git 历史记录),作者:Ayushya Jaiswal,我将在这篇文章用于存档目的。
tl;dr:您实际上是在(安全地)重新建立存储库并仅提取您想要的文件。然后将 git 历史记录从您的 rebased 存储库中提取到您当前正在处理的任何存储库中。
开始之前的注意事项:
如果您想要具体文件。我将在下面引用的指南中添加在此步骤中添加的免责声明。
以下是文章中引用的文本:
准备好从存储库 A 移动文件。
第 1 步:制作存储库 A 的副本,因为以下步骤对此副本进行了重大更改,您不应推送这些更改!
第 2 步:转到该目录。
第 3 步:为避免意外进行任何远程更改(例如通过推送),请删除原始存储库的链接。
这是要修改的步骤,通过执行 此处只会推断您所需的文件。其余的应该是一样的。
在您的情况下,它会是这样的:
第 4 步:浏览您的历史记录和文件,删除FOLDER_TO_KEEP
中不存在的所有内容。结果是FOLDER_TO_KEEP
的内容涌入存储库 A 的底部。第 5 步:清理不需要的数据。
第 6 步:将所有文件和目录移动到要推送到存储库 B 的 NEW_FOLDER。
或者,您可以使用 GUI 将所有文件和目录拖到 NEW_FOLDER。
第 7 步:添加更改并提交。
将文件合并到新的存储库 B 中。
第 1 步:如果您还没有存储库 B,请制作一份副本。
第 2 步:转到该目录。
第 3 步:创建到存储库 A 的远程连接,作为存储库 B 中的分支。
第 4 步:从此分支中提取文件和历史记录(仅包含要移动的目录) ) 到存储库 B。
第 5 步:删除与存储库 A 的远程连接。
第 6 步:最后,推送更改
您可以删除两个克隆的存储库。
随历史记录更改的文件现在可在存储库 B 中在线获取。
There's no simple fix, but there is a really well written guide here (Move files from one repository to another, preserving git history) by Ayushya Jaiswal and I'll quote it in this post for archival purposes.
tl;dr: You're essentially re-basing a repo (safely) and extracting just the file(s) that you want. Then pulling the git history from your rebased repo into whatever repo you're currently working on.
Notes before you get started:
You'll need https://stackoverflow.com/a/56334887/929999 for this as well, if you want a specific file. I'll add a disclaimer below in the quoted guide where to add in this step.
Here's the quoted text from the article:
Getting files ready to move from Repository A.
Step 1: Make a copy of repository A as the following steps make major changes to this copy which you should not push!
Step 2: Go to that directory.
Step 3: To avoid accidentally making any remote changes (eg. by pushing), delete the link to the original repository.
This is the step to modify, modify it by doing the
git filter-branch --prune-empty ... $FILES
step from here instead, that will extrapolate only your desired files. The rest should be the same.In your case, it would be something like this:
Step 4: Go through your history and files, removing anything that is not inFOLDER_TO_KEEP
. The result is the contents ofFOLDER_TO_KEEP
spewed out into the base of repository A.Step 5: Clean the unwanted data.
Step 6: Move all the files and directories to a NEW_FOLDER which you want to push to repository B.
Alternatively, you can drag all the files and directory to the NEW_FOLDER using GUI.
Step 7: Add the changes and commit them.
Merge the files into the new repository B.
Step 1: Make a copy of repository B if you don’t have one already.
Step 2: Go to that directory.
Step 3: Create a remote connection to repository A as a branch in repository B.
Step 4: Pull files and history from this branch (containing only the directory you want to move) into repository B.
Step 5: Remove the remote connection to repository A.
Step 6: Finally, push the changes
You can delete both the cloned repositories.
The files changes with history are now available online in repository B.
您必须直接复制该文件。 Git 处理整个存储库,而不是其中的单个文件。
我想您可以将存储库 2 设置为远程存储库,获取(而不是拉取)其分支,然后使用 git checkout 从该分支中获取文件,但是解决方案可能会很混乱。
You'll have to copy the file directly. Git deals with whole repositories, not single files within them.
I suppose you could set Repository 2 up as a remote repository, fetch (not pull) its branches, then use
git checkout
to grab a file out of that branch, but that solution could be messy.从具有完整 git 历史记录的 git 存储库中提取文件或文件夹:
A really simple process is described at Extracting a file or a folder from a git repository with full git history: