How to keep only head changes in a git rebase

For the sake of completeness, this is what I learn from the previous answer and comments in current Q&A (credit goes to their authors):

  • If you are willing to start over and choose same side for ALL the commits, the current chosen answer git rebase --abort and then git rebase -X ours upstream can do the trick.
  • But I guess in practice you won't want to blindly use either ours or theirs without looking into each commits. You would want to make a case-by-case decision for each commit, when "in the middle of a lengthy rebase". So, your actual options will be:

    1. To use the upstream:

      git checkout --ours path/to/a/specific/file
      git add path/to/a/specific/file
      

      or even better, in this case you simply use this:

      git reset HEAD path/to/a/specific/file
      
    2. Use your feature branch:

      git checkout --theirs path/to/a/specific/file
      
    3. or do it in the manual way to address each <<<< ... ==== ... >>>> in your editor.

PS: The ours and theirs have special meaning when doing rebase.:

Note that during git rebase and git pull --rebase, ours and theirs may appear swapped; --ours gives the version from the branch the changes are rebased onto, while --theirs gives the version from the branch that holds your work that is being rebased.

This is because rebase is used in a workflow that treats the history at the remote as the shared canonical one, and treats the work done on the branch you are rebasing as the third-party work to be integrated, and you are temporarily assuming the role of the keeper of the canonical history during the rebase. As the keeper of the canonical history, you need to view the history from the remote as ours (i.e. "our shared canonical history"), while what you did on your side branch as theirs (i.e. "one contributor’s work on top of it").


If you are willing to start the rebase over (git rebase --abort), then this should do what you need:

git rebase -X ours upstream

where upstream is the branch you are rebasing onto.

As noted in this answer and elsewhere, the ours vs. theirs labels are slightly more confusing for rebasing than for merging. After starting a rebase, Git creates an anonymous branch and starts applying commits to it. Since ours means "keep changes from the current branch", that current branch will be HEAD, which contains upstream and any changes already applied by rebase.

Tags:

Git

Git Rebase