简体   繁体   中英

Git rebase of unrelated histories

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>

  1. Is that expected behaviour – and if so, why? I would have expected a "can not rebase unrelated histories" error much like with git merge instead of this behaviour.
  2. How can I fix the git rebase command to perform the rebase as described above?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM