git格式点和分支冲突

发布于 2025-02-11 15:07:49 字数 2102 浏览 1 评论 0原文

我需要从一个仓库(repo-a)创建补丁,然后将它们合并到另一个仓库(repo-b)。这些补丁需要包含自己的承诺,因此我发现git格式patchgit am是一个完美的合适。

我们在repo-a中使用多个分支,并且不时地从两个不同的分支中添加文件,并将其合并到main中。

git am不喜欢此。这是问题的完整示例;

mkdir repo-a; cd repo-a

git init

touch randomfile
git add randomfile
git commit -m "Adding an initial file"

git checkout -b anotherbranch
touch a-file
git add a-file
git commit -m "Adding a-file from anotherbranch"

git checkout main
touch a-file
git add a-file
git commit -m "Adding a-file from main"

git merge anotherbranch

git format-patch -o patches HEAD~2
# patches/0001-Adding-a-file-from-anotherbranch.patch
# patches/0002-Adding-a-file-from-main.patch

cd ..
mkdir repo-b; cd repo-b
git init
cp -r ../repo-a/patches ./

git am patches/*
# Applying: Adding a-file from anotherbranch
# applying to an empty history
# Applying: Adding a-file from main
# error: a-file: already exists in index
# Patch failed at 0002 Adding a-file from main
# hint: Use 'git am --show-current-patch=diff' to see the failed patch
# When you have resolved this problem, run "git am --continue".
# If you prefer to skip this patch, run "git am --skip" instead.
# To restore the original branch and stop patching, run "git am --abort".

以上失败是因为git已经具有文件。通常,Git很高兴,并尽其所能添加补丁。.

是否有办法做到这一点,并保持GIT提交?我不想创建一个大补丁,我也想要提交。

我已经阅读 https://git-scm.com/docs/git-format-format-patch- 在此处输入链接描述。我还尝试了一些随机选项(例如- Find-copies-Harder),但没有任何运气。

一些其他信息:

  • REBO-B使用CP -Rrm -rf .git和Mose-other-files和一个新git Init
    • Rebo-B因此是主要存储库,Rebo-A只是一个被剥离的版本。
    • 所有这一切的主要目标是让某人能够在repo-B上工作,但他们没有访问Rebo-B中的所有内容。将repo-b复制到repo-a之后,我正在执行很多文件消耗,但仅删除。

I need to create patches from one repo (repo-a) and merge them into another (repo-b). These patches needs to contain the commits themself, so I found that git format-patch and git am is a perfect fit.

We are using multiple branches in repo-a and from time to time, files are added from two different branches and merged into main.

git am doesn't like this.. Here is a full example of the problem;

mkdir repo-a; cd repo-a

git init

touch randomfile
git add randomfile
git commit -m "Adding an initial file"

git checkout -b anotherbranch
touch a-file
git add a-file
git commit -m "Adding a-file from anotherbranch"

git checkout main
touch a-file
git add a-file
git commit -m "Adding a-file from main"

git merge anotherbranch

git format-patch -o patches HEAD~2
# patches/0001-Adding-a-file-from-anotherbranch.patch
# patches/0002-Adding-a-file-from-main.patch

cd ..
mkdir repo-b; cd repo-b
git init
cp -r ../repo-a/patches ./

git am patches/*
# Applying: Adding a-file from anotherbranch
# applying to an empty history
# Applying: Adding a-file from main
# error: a-file: already exists in index
# Patch failed at 0002 Adding a-file from main
# hint: Use 'git am --show-current-patch=diff' to see the failed patch
# When you have resolved this problem, run "git am --continue".
# If you prefer to skip this patch, run "git am --skip" instead.
# To restore the original branch and stop patching, run "git am --abort".

The above fails because git already has the file. Normally, git is happy and adds the patches as it should..

Is there a way to do this, and keep the git commits? I don't want to create one big patch, I want the commits as well.

I have read https://git-scm.com/docs/git-format-patch and enter link description here. I have also tried some random options (like --find-copies-harder), but without any luck.

Some additional info:

  • rebo-b is made from repo-a using cp -r, rm -rf .git and-some-other-files and a new git init.
    • rebo-b is therefor the main repo, rebo-a is just a stripped down version.
    • The main goal of all this is to have someone be able to work on repo-b, but without them having access to everything that is in rebo-b. After copying repo-b to repo-a, I am doing a lot of file-deletions, but only deletions.

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

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

发布评论

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

评论(3

和我恋爱吧 2025-02-18 15:07:50

git格式patch非常适合提取提交的线性历史记录,但不是为了合并:

描述
用其“补丁” ...

[...]
警告
请注意,即使它们属于请求范围的一部分,格式patch将省略合并

  • 如您在示例中所看到的:

为您的每个提交创建一个补丁程序enternbranchmain,但没有用于合并提交,如果您检查了Pacthes,则他们不包含信息以指出他们俩都有相同的父母。

这不足以自动重建与合并的历史记录。


在保存分支和合并的同时,可能有几种策略可以从repo-arepo-b,但我们需要更多的上下文来为您提供更精确的上下文回答。

例如:一种方法可以是将repo-a添加为repo-b中的远程,并使用git rebase -r(<代码> -r 是的简短 - rebase-merges)或git> git filter-repo的某些子命令到端口完整的历史记录块。


如果要从repo-a中删除文件,但可以显示其余文件的历史记录,则可以使用git filter-reporepo-a repo-a变成剥离版本。上行是该命令以可重复的方式创建提交,因此新提交的历史记录与先前的提取相匹配。

git format-patch is well suited to extract a linear history of commits, but not for merges :

DESCRIPTION
Prepare each non-merge commit with its "patch" ...

[...]
CAVEATS
Note that format-patch will omit merge commits from the output, even if they are part of the requested range.

  • as you can see in your example :

a patch is created for each of your commits on anotherbranch and main, but none for the merge commit, and if you inspect the pacthes, they do not include information to state that they both have the same parent.

This is not enough information to automatically rebuild a history with merges.


There may be several strategies to port commits from repo-a to repo-b while preserving branching and merges, but we would need a bit more context to give you a more precise answer.

For example : one way could be to add repo-a as a remote in repo-b, and use git rebase -r (-r is short for --rebase-merges) or some subcommand of git filter-repo to port complete chunks of history.


If you want to remove files from repo-a, but are okay with presenting the history for the remaining files, you could use git filter-repo to turn repo-a into a stripped down version. The upside would be that this command creates commits in a repeatable way, so the history for new commits would match the previous extraction.

如日中天 2025-02-18 15:07:50

我找到了一种以我想要的方式来做到这一点的方法。

git remote add otherrepo ../otherrepo
git fetch otherrepo

# If you want to fetch lfs objects as well
git lfs fetch --all otherrepo

# -X subtree: Or you will get a lot of merge-conflicts, some of them with empty lines.
# --signoff: To add signoff on the merge-commit (not the merged commits)
# --allow-unrelated-histories: This is the main trick!
git merge -X subtree --signoff --allow-unrelated-histories otherrepo/main

整个历史和一切都应该奏效。合并 - 命令包含合并的内容,历史,图形,作者和一切看起来都不错。

I found a way to do this in the way I wanted.

git remote add otherrepo ../otherrepo
git fetch otherrepo

# If you want to fetch lfs objects as well
git lfs fetch --all otherrepo

# -X subtree: Or you will get a lot of merge-conflicts, some of them with empty lines.
# --signoff: To add signoff on the merge-commit (not the merged commits)
# --allow-unrelated-histories: This is the main trick!
git merge -X subtree --signoff --allow-unrelated-histories otherrepo/main

The whole history and everything works as it should. The merge-commit contains what is merged, and history, graph, authors and everything looks good.

欢烬 2025-02-18 15:07:50
  git合并另一只
 

我宁愿 rebase 在目标分支顶部(在主的顶部)上的分支,然后在该新的分支头和Main之间进行补丁(而不是head 〜2

git switch anotherbranch
git rebase main
git format-patch -o patches main
git merge anotherbranch

I would rather rebase the branch on top of the target branch (on top of main), before making a patch between that new branch HEAD and main (instead of HEAD~2)

git switch anotherbranch
git rebase main
git format-patch -o patches main
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文