Mike
Mike

Reputation: 333

How to add a commit in current branch to change content to a specific commit in another branch

I have branchA and branchB, both of which are checked out from master branch.

branchA has commits commitA1 and commitA2 and branchB has commits commitB1 and commitB2. Changes on both branches have overlaps.

I want to add a commit commitA3 in branchA so that branchA and commitB1 in branchB would look exactly the same without any conflicts resolving. How can I create commitA3?

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

Upvotes: 1

Views: 183

Answers (2)

root
root

Reputation: 6058

Another option is:

git checkout branchA
git rm -r .
git checkout commitB1 -- .
git commit -am commitA3

Here:

$ 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

Upvotes: 1

Mike
Mike

Reputation: 333

In local repo, I can perform:

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

In GH actions, I implemented (through cloning another repo):

execSync(`git diff origin/${branch} ${commit}> ${diffPatch}`);
execSync(`git clone --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, encoding: 'utf8' });

Upvotes: 0

Related Questions