我可以只从另一个 git 存储库中提取某些文件吗?

发布于 2024-08-18 04:51:09 字数 235 浏览 7 评论 0 原文

例如,假设我有存储库 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

美胚控场 2024-08-25 04:51:09

您可以使用 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 then git 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

亣腦蒛氧 2024-08-25 04:51:09

没有简单的修复方法,但有一个写得很好的指南 这里(将文件从一个存储库移动到另一个存储库,保留 git 历史记录),作者:Ayushya Jaiswal,我将在这篇文章用于存档目的。

tl;dr:您实际上是在(安全地)重新建立存储库并仅提取您想要的文件。然后将 git 历史记录从您的 rebased 存储库中提取到您当前正在处理的任何存储库中。

开始之前的注意事项
如果您想要具体文件。我将在下面引用的指南中添加在此步骤中添加的免责声明。

以下是文章中引用的文本:


准备好从存储库 A 移动文件。

第 1 步:制作存储库 A 的副本,因为以下步骤对此副本进行了重大更改,您不应推送这些更改!

mkdir cloneA
cd cloneA
git clone --branch <branch> --origin origin --progress \
  -v <git repository A url>
# eg. git clone --branch master --origin origin --progress \
#   -v https://github.com/username/myproject.git
# (assuming myprojects is the repository you want to copy from)

第 2 步:转到该目录。

cd <git repository A directory>
#  eg. cd myproject
# Folder Path is ~/cloneA/myproject

第 3 步:为避免意外进行任何远程更改(例如通过推送),请删除原始存储库的链接。

git remote rm origin

这是要修改的步骤,通过执行 此处只会推断您所需的文件。其余的应该是一样的。

在您的情况下,它会是这样的:

FILES='/a/b/c/d'
git filter-branch --prune-empty --index-filter "
                        git read-tree --empty
                        git reset \$GIT_COMMIT -- $FILES
                " \
        -- --all -- $FILES

第 4 步:浏览您的历史记录和文件,删除 FOLDER_TO_KEEP 中不存在的所有内容。结果是 FOLDER_TO_KEEP 的内容涌入存储库 A 的底部。

git filter-branch --subdirectory-filter <directory> -- --all
# eg. git filter-branch --subdirectory-filter subfolder1/subfolder2/FOLDER_TO_KEEP -- --all

第 5 步:清理不需要的数据。

git reset --hard
git gc --aggressive 
git prune
git clean -fd

第 6 步:将所有文件和目录移动到要推送到存储库 B 的 NEW_FOLDER。

mkdir <base directory>
#eg mkdir NEW_FOLDER
mv * <base directory>
#eg mv * NEW_FOLDER

或者,您可以使用 GUI 将所有文件和目录拖到 NEW_FOLDER。

第 7 步:添加更改并提交。

git add .
git commit

将文件合并到新的存储库 B 中。

第 1 步:如果您还没有存储库 B,请制作一份副本。

mkdir cloneB
cd cloneB
git clone <git repository B url>
# eg. git clone 
https://github.com/username/newproject.git

第 2 步:转到该目录。

cd <git repository B directory>
#  eg. cd newproject
# Folder Path is ~/cloneB/newproject

第 3 步:创建到存储库 A 的远程连接,作为存储库 B 中的分支。

git remote add repo-A <git repository A directory>
# (repo-A can be anything - it's just a random name)

# eg. git remote add repo-A ~/cloneA/myproject

第 4 步:从此分支中提取文件和历史记录(仅包含要移动的目录) ) 到存储库 B。

git pull repo-A master --allow-unrelated-histories
# This merges master from repository A into repository B

第 5 步:删除与存储库 A 的远程连接。

git remote rm repo-A

第 6 步:最后,推送更改

git push

您可以删除两个克隆的存储库。
随历史记录更改的文件现在可在存储库 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!

mkdir cloneA
cd cloneA
git clone --branch <branch> --origin origin --progress \
  -v <git repository A url>
# eg. git clone --branch master --origin origin --progress \
#   -v https://github.com/username/myproject.git
# (assuming myprojects is the repository you want to copy from)

Step 2: Go to that directory.

cd <git repository A directory>
#  eg. cd myproject
# Folder Path is ~/cloneA/myproject

Step 3: To avoid accidentally making any remote changes (eg. by pushing), delete the link to the original repository.

git remote rm origin

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:

FILES='/a/b/c/d'
git filter-branch --prune-empty --index-filter "
                        git read-tree --empty
                        git reset \$GIT_COMMIT -- $FILES
                " \
        -- --all -- $FILES

Step 4: Go through your history and files, removing anything that is not in FOLDER_TO_KEEP. The result is the contents of FOLDER_TO_KEEP spewed out into the base of repository A.

git filter-branch --subdirectory-filter <directory> -- --all
# eg. git filter-branch --subdirectory-filter subfolder1/subfolder2/FOLDER_TO_KEEP -- --all

Step 5: Clean the unwanted data.

git reset --hard
git gc --aggressive 
git prune
git clean -fd

Step 6: Move all the files and directories to a NEW_FOLDER which you want to push to repository B.

mkdir <base directory>
#eg mkdir NEW_FOLDER
mv * <base directory>
#eg mv * NEW_FOLDER

Alternatively, you can drag all the files and directory to the NEW_FOLDER using GUI.

Step 7: Add the changes and commit them.

git add .
git commit

Merge the files into the new repository B.

Step 1: Make a copy of repository B if you don’t have one already.

mkdir cloneB
cd cloneB
git clone <git repository B url>
# eg. git clone 
https://github.com/username/newproject.git

Step 2: Go to that directory.

cd <git repository B directory>
#  eg. cd newproject
# Folder Path is ~/cloneB/newproject

Step 3: Create a remote connection to repository A as a branch in repository B.

git remote add repo-A <git repository A directory>
# (repo-A can be anything - it's just a random name)

# eg. git remote add repo-A ~/cloneA/myproject

Step 4: Pull files and history from this branch (containing only the directory you want to move) into repository B.

git pull repo-A master --allow-unrelated-histories
# This merges master from repository A into repository B

Step 5: Remove the remote connection to repository A.

git remote rm repo-A

Step 6: Finally, push the changes

git push

You can delete both the cloned repositories.
The files changes with history are now available online in repository B.

你在看孤独的风景 2024-08-25 04:51:09

您必须直接复制该文件。 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.

羅雙樹 2024-08-25 04:51:09

从具有完整 git 历史记录的 git 存储库中提取文件或文件夹

  1. 创建补丁文件
    cd ~/repository/path
    git log --pretty=email --patch-with-stat --reverse --full-index --binary --path/to/file_or_folder > /tmp/补丁
    
  2. 如果需要,可以选择替换路径
  3. 应用提交到新存储库
    cd ~/another_repository/path
    git am <; /tmp/补丁
    

A really simple process is described at Extracting a file or a folder from a git repository with full git history:

  1. Create a patch file
    cd ~/repository/path
    git log --pretty=email --patch-with-stat --reverse --full-index --binary -- path/to/file_or_folder > /tmp/patch
    
  2. Optionally replace paths if required
  3. Apply commits to new repository
    cd ~/another_repository/path
    git am < /tmp/patch
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文