Reputation: 16870
I am trying to do a rebase with two unrelated branches in Git.
| A A A A A | => | A A A A A B B |
| B B |
However, in doing so, all I am left with is just the commits from branch M
.
Demonstration:
$ git init && git ci -m "MASTER branch" --allow-empty
[master (root-commit) 05abfe5] MASTER branch
$ git co --orphan FEATURE && git ci -m "FEATURE branch" --allow-empty
Switched to a new branch 'FEATURE'
[FEATURE (root-commit) 122671d] FEATURE branch
$ git rebase master
First, rewinding head to replay your work on top of it...
$ git lg
* 05abfe5 - (HEAD -> FEATURE, master) MASTER branch (18 seconds ago) <Niklas Rosenstein>
What I would expect from the last command is
$ git lg
* 122671d - (HEAD -> FEATURE) FEATURE branch (1 second ago) <Niklas Rosenstein>
* 05abfe5 - (master) MASTER branch (18 seconds ago) <Niklas Rosenstein>
git merge
instead of this behaviour.git rebase
command to perform the rebase as described above?Upvotes: 3
Views: 1056
Reputation: 488013
As ElpieKay noted in a comment, you need --keep-empty
for this particular example. Git in general has a tendency to throw out, or not make in the first place, commits that make no change to the tree. There's no reason it has to do that—two commits in a row that use the same tree is a normal case; it occurs with git merge -s ours
for instance—but it does.
When using git rebase
in its non-interactive, non-cherry-picking modes, Git uses git format-patch
to turn each commit into a patch, and git am
(apply mailbox-formatted patches) to re-apply them, to make the copies of the original commits. The format-patch command is unable to format a "do nothing" patch, so when you use -k
or --keep-empty
, Git switches to using git cherry-pick
to copy the commits.
There are other options that force rebase to use cherry-pick, such as --interactive
and --merge
. Cherry-picking also defaults to omitting "empty" commits and also requires --keep-empty
, whether invoked directly, or from this other kind of rebase.
Upvotes: 4