git格式点和分支冲突
我需要从一个仓库(repo-a
)创建补丁,然后将它们合并到另一个仓库(repo-b
)。这些补丁需要包含自己的承诺,因此我发现git格式patch
和git 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 -R
,rm -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 newgit 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
git格式patch
非常适合提取提交的线性历史记录,但不是为了合并:git帮助格式patch
,重点是我的):为您的每个提交创建一个补丁程序
enternbranch
和main
,但没有用于合并提交,如果您检查了Pacthes,则他们不包含信息以指出他们俩都有相同的父母。这不足以自动重建与合并的历史记录。
在保存分支和合并的同时,可能有几种策略可以从
repo-a
到repo-b
,但我们需要更多的上下文来为您提供更精确的上下文回答。例如:一种方法可以是将
repo-a
添加为repo-b
中的远程,并使用git rebase -r
(<代码> -r 是的简短 - rebase-merges
)或git> git filter-repo
的某些子命令到端口完整的历史记录块。如果要从repo-a中删除文件,但可以显示其余文件的历史记录,则可以使用
git filter-repo
将repo-a repo-a
变成剥离版本。上行是该命令以可重复的方式创建提交,因此新提交的历史记录与先前的提取相匹配。git format-patch
is well suited to extract a linear history of commits, but not for merges :git help format-patch
, emphasis mine):a patch is created for each of your commits on
anotherbranch
andmain
, 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
torepo-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 inrepo-b
, and usegit rebase -r
(-r
is short for--rebase-merges
) or some subcommand ofgit 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 turnrepo-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.我找到了一种以我想要的方式来做到这一点的方法。
整个历史和一切都应该奏效。合并 - 命令包含合并的内容,历史,图形,作者和一切看起来都不错。
I found a way to do this in the way I wanted.
The whole history and everything works as it should. The merge-commit contains what is merged, and history, graph, authors and everything looks good.
我宁愿 rebase 在目标分支顶部(在主的顶部)上的分支,然后在该新的分支头和Main之间进行补丁(而不是
head 〜2
)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
)