How to get "their" changes in the middle of conflicting Git rebase?

205,910

Solution 1

You want to use:

git checkout --ours foo/bar.java
git add foo/bar.java

If you rebase a branch feature_x against main (i.e. running git rebase main while on branch feature_x), during rebasing ours refers to main and theirs to feature_x.

As pointed out in the git-rebase docs:

Note that a rebase merge works by replaying each commit from the working branch on top of the branch. Because of this, when a merge conflict happens, the side reported as ours is the so-far rebased series, starting with <upstream>, and theirs is the working branch. In other words, the sides are swapped.

For further details read this thread.

Solution 2

If you want to pull a particular file from another branch just do

git checkout branch1 -- filenamefoo.txt

This will pull a version of the file from one branch into the current tree

Share:
205,910
febot
Author by

febot

I am. (See my LinkedIn profile. Open to job offers for USA, Germany, Austria, Switzerland). 2012 update: I'm a company-man, a team-player, a paradigm-shifter and a core-competancy-synergizer. :) PS: I worked for Red Hat / JBoss, so my answers may be biased. Currently work for Swiss Re.

Updated on July 08, 2022

Comments

  • febot
    febot almost 2 years

    I have conflicting branches, feature_x branched from main.

    Let's say when rebasing feature_x on current main, while resolving conflicts, I decide to take some (not all) of "their" (i.e. main) files as-is. How do I do that?

    I tried:

    git checkout main:foo/bar.java
    fatal: reference is not a tree: TS-modules-tmp:foo/bar.java
      
    git checkout refs/heads/main:foo/bar.java
    fatal: reference is not a tree: refs/heads/TS-modules-tmp:foo/bar.java
    
  • Clintm
    Clintm almost 11 years
    This would probably be a bad idea in the middle of a rebase as it would pull the file from the head of that branch not at the detached head point you would be at in a conflicted rebase state
  • laurent
    laurent almost 4 years
    I'm sure there's a good reason why merge and rebase have opposite meaning for "ours" and "theirs", but consistency would have been so much better.
  • akostadinov
    akostadinov over 3 years
    @Clintm, on the other hand it is a generic answer that you can pull the file from wherever you want. branch1 would not be a particularly good idea as noted. But one can use a desired commit id.
  • Siddhartha
    Siddhartha over 3 years
    So their in OPs question corresponds to --ours in the actual command.
  • ut9081
    ut9081 over 3 years
    Note that --ours also corresponds (more intuitively) to the 'Current Changes' in your IDE when resolving conflicts, whereas --theirs corresponds to 'Incoming Changes'
  • Michael Trouw
    Michael Trouw almost 3 years
    @ut9081 in your /VScode/ IDE ;)