Git 如何在当前分支中添加提交以将内容更改为另一分支中的特定提交

Git 如何在当前分支中添加提交以将内容更改为另一分支中的特定提交,git,Git,我有branchA和branchB,它们都是从master分支签出的 branchA有提交commitA1和commitA2和branchB有提交commitB1和commitB2。两个分支上的更改都有重叠 我想在branchA中添加一个commitcommitA3,这样branchA和commitB1中的commitA3看起来完全一样,没有任何冲突解决。如何创建commitA3 master------------------------------------| |---- branch

我有
branchA
branchB
,它们都是从
master
分支签出的

branchA
有提交
commitA1
commitA2
branchB
有提交
commitB1
commitB2
。两个分支上的更改都有重叠

我想在
branchA
中添加一个commit
commitA3
,这样
branchA
commitB1
中的
commitA3
看起来完全一样,没有任何冲突解决。如何创建
commitA3

master------------------------------------|
  |---- branchA                           |---- branchB
          |---- commitA1              s~~~~~>     |---- commitB1
          |---- commitA2 (current)    s           |---- commitB2 (current)
          |---- commitA3 ~~~~~~~~~~~~~s

在本地回购中,我可以执行:

git diff origin/branchA commitB1> /tmp/commit_branch_diff.patch
git checkout branchA
git apply /tmp/commit_branch_diff.patch
git add -all .
git commit -m "commitB1"
git push origin branchA
在GH行动中,我实施了(通过克隆另一个回购协议):

execSync(`git diff origin/${branch}${commit}>${diffPatch}`);
execSync(`gitclone--quiet--branch${branch}--depth 1${origin}${cwd}`);
execSync('git config--local user.name bot',{cwd});
execSync('git config--local user.email”“,{cwd});
execSync(`git apply${diffPatch}`,{cwd});
execSync('git add--all.',{cwd});
execSync(`git commit-m${commit}`,{cwd});
execSync(`git push${origin}${branch}`,{cwd,编码:'utf8'});
另一个选项是:

git checkout branchA
git rm -r .
git checkout commitB1 -- .
git commit -am commitA3
在这里:


通常情况并非如此,如果您希望合并而不发生冲突,则其中一个分支应与master合并,然后另一个分支与master重新基化。@mamounothman这不是在日常工作中使用的,实际上我想编写一个自定义github操作,它正是这样做的
git checkout branchA
git rm -r .
git checkout commitB1 -- .
git commit -am commitA3
$ git diff master..branchA
diff --git a/y b/y
new file mode 100644
index 0000000..975fbec
--- /dev/null
+++ b/y
@@ -0,0 +1 @@
+y
diff --git a/z b/z
new file mode 100644
index 0000000..b680253
--- /dev/null
+++ b/z
@@ -0,0 +1 @@
+z
$ git diff master..branchB
diff --git a/w b/w
new file mode 100644
index 0000000..e556b83
--- /dev/null
+++ b/w
@@ -0,0 +1 @@
+w
diff --git a/z b/z
new file mode 100644
index 0000000..67d0c15
--- /dev/null
+++ b/z
@@ -0,0 +1 @@
+z2
$ git diff branchA branchB
diff --git a/w b/w
new file mode 100644
index 0000000..e556b83
--- /dev/null
+++ b/w
@@ -0,0 +1 @@
+w
diff --git a/y b/y
deleted file mode 100644
index 975fbec..0000000
--- a/y
+++ /dev/null
@@ -1 +0,0 @@
-y
diff --git a/z b/z
index b680253..67d0c15 100644
--- a/z
+++ b/z
@@ -1 +1 @@
-z
+z2
$ git show-branch master branchA branchB commitB1 
* [master] master
 ! [branchA] commitA2
  ! [branchB] commitB2
   ! [commitB1] commitB1
----
  +  [branchB] commitB2
  ++ [commitB1] commitB1
 +   [branchA] commitA2
 +   [branchA^] commitA1
*+++ [master] master
$ git checkout branchA
Switched to branch 'branchA'
$ git rm -r .
rm 'x'
rm 'y'
rm 'z'
$ git checkout commitB1 -- .
$ git commit -am commitA3
[branchA 3b1b4d1] commitA3
 2 files changed, 1 insertion(+), 2 deletions(-)
 delete mode 100644 y
$ git diff branchA commitB1 && echo same
same
$ git show-branch master branchA branchB commitB1 
! [master] master
 * [branchA] commitA3
  ! [branchB] commitB2
   ! [commitB1] commitB1
----
 *   [branchA] commitA3
 *   [branchA^] commitA2
 *   [branchA~2] commitA1
  +  [branchB] commitB2
  ++ [commitB1] commitB1
+*++ [master] master