Skip to content

Instantly share code, notes, and snippets.

@MarkLodato
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkLodato/8f57eada601bc0dbee34 to your computer and use it in GitHub Desktop.
Save MarkLodato/8f57eada601bc0dbee34 to your computer and use it in GitHub Desktop.
Response to Cupcake's question in http://stackoverflow.com/a/15802324/303425

Create the repository:

▶ git init

Create the first branch:

▶ echo foo > one
▶ git add one
▶ git commit -m A
[master (root-commit) 7edac5e] A
 1 file changed, 1 insertion(+)
 create mode 100644 one
▶ echo bar > two
▶ git add two   
▶ git commit -m D
[master 68c3082] D
 1 file changed, 1 insertion(+)
 create mode 100644 two
▶ ls
one  two

Create the second branch, where the initial commit (K) is the same as the final commit of master (D), except that a file has been deleted:

▶ git checkout --orphan other 
Switched to a new branch 'other'
▶ git add one
▶ git rm -f two
rm 'two'
▶ git commit -m K
[other (root-commit) 4fe7f5c] K
 1 file changed, 1 insertion(+)
 create mode 100644 one
▶ echo baz > three
▶ git add three
▶ git commit -m N
[other d388bc3] N
 1 file changed, 1 insertion(+)
 create mode 100644 three
▶ ls
one  three

Merge the branches using git rebase.

▶ git checkout -b merged                              
Switched to a new branch 'merged'
▶ git rebase --onto master --root
First, rewinding head to replay your work on top of it...
Applying: N
▶ ls
one  three  two
▶ git log --oneline merged
aa0e3f6 N
68c3082 D
7edac5e A

Note that the commit, K, that deleted file "two" has been eliminated, and "two" is incorrectly present in the merged branch even though it did not appear in "other" branch.

The full logs, for the curious:

▶ git log -p master
commit 68c30821a4a816c9a829ea9608c1514a2411d4d8
Author: Mark Lodato <lodato@google.com>
Date:   2014-07-22 22:50:22 -0400

    D

diff --git a/two b/two
new file mode 100644
index 0000000..5716ca5
--- /dev/null
+++ b/two
@@ -0,0 +1 @@
+bar

commit 7edac5e8ded52e3920ad42fb627d7e98097b8c35
Author: Mark Lodato <lodato@google.com>
Date:   2014-07-22 22:50:12 -0400

    A

diff --git a/one b/one
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/one
@@ -0,0 +1 @@
+foo
▶ git log -p other 
commit d388bc3159c0630618c73ef99e1640322b842c63
Author: Mark Lodato <lodato@google.com>
Date:   2014-07-22 22:52:14 -0400

    N

diff --git a/three b/three
new file mode 100644
index 0000000..7601807
--- /dev/null
+++ b/three
@@ -0,0 +1 @@
+baz

commit 4fe7f5c6c4a827a014760919b9504b4193d5ab00
Author: Mark Lodato <lodato@google.com>
Date:   2014-07-22 22:51:49 -0400

    K

diff --git a/one b/one
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/one
@@ -0,0 +1 @@
+foo
▶ git log -p merged
commit aa0e3f6e9aa94f926faf11ccf2e2e0c9cb1df996
Author: Mark Lodato <lodato@google.com>
Date:   2014-07-22 22:52:14 -0400

    N

diff --git a/three b/three
new file mode 100644
index 0000000..7601807
--- /dev/null
+++ b/three
@@ -0,0 +1 @@
+baz

commit 68c30821a4a816c9a829ea9608c1514a2411d4d8
Author: Mark Lodato <lodato@google.com>
Date:   2014-07-22 22:50:22 -0400

    D

diff --git a/two b/two
new file mode 100644
index 0000000..5716ca5
--- /dev/null
+++ b/two
@@ -0,0 +1 @@
+bar

commit 7edac5e8ded52e3920ad42fb627d7e98097b8c35
Author: Mark Lodato <lodato@google.com>
Date:   2014-07-22 22:50:12 -0400

    A

diff --git a/one b/one
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/one
@@ -0,0 +1 @@
+foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment