首页 > 解决方案 > 如何在当前分支中添加提交以将内容更改为另一个分支中的特定提交

问题描述

我有branchAand branchB,两者都是从master分支中签出的。

branchA有提交commitA1和有提交commitA2和. 两个分支上的更改有重叠。branchBcommitB1commitB2

我想添加一个提交commitA3branchA以便在没有任何冲突解决branchA的情况下看起来完全相同。我该如何创建?commitB1branchBcommitA3

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

标签: git

解决方案


另一种选择是:

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


推荐阅读