conflict - git rebase, keeping track of 'local' and 'remote'

ID : 20297

viewed : 10

Tags : gitconflictrebasegit

Top 5 Answer for conflict - git rebase, keeping track of 'local' and 'remote'

vote vote



To summarize (As Benubird comments), when:

git checkout A git rebase   B    # rebase A on top of B 
  • local is B (rebase onto),
  • remote is A


git checkout A git merge    B    # merge B into A 
  • local is A (merge into),
  • remote is B

A rebase switches ours (current branch before rebase starts) and theirs (the branch on top of which you want to rebase).

kutschkem points out that, in a GUI mergetool context:

  • local references the partially rebased commits: "ours" (the upstream branch)
  • remote refers to the incoming changes: "theirs" - the current branch before the rebase.

See illustrations in the last part of this answer.

Inversion when rebase

The confusion might be related to the inversion of ours and theirs during a rebase.
(relevant extracts)

git rebase man page:

Note that a rebase merge works by replaying each commit from the working branch on top of the <upstream> 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.

Inversion illustrated

On a merge

x--x--x--x--x(*) <- current branch B ('*'=HEAD)     \      \       \--y--y--y <- other branch to merge 

, we don't change the current branch 'B', so what we have is still what we were working on (and we merge from another branch)

x--x--x--x--x---------o(*)  MERGE, still on branch B     \       ^        /      \     ours     /       \            /        --y--y--y--/                  ^               their 

On a rebase:

But on a rebase, we switch side because the first thing a rebase does is to checkout the upstream branch! (to replay the current commits on top of it)

x--x--x--x--x(*) <- current branch B     \      \       \--y--y--y <- upstream branch 

A git rebase upstream will first change HEAD of B to the upstream branch HEAD (hence the switch of 'ours' and 'theirs' compared to the previous "current" working branch.)

x--x--x--x--x <- former "current" branch, new "theirs"     \      \       \--y--y--y(*) <- upstream branch with B reset on it,                          new "ours", to replay x's on it 

, and then the rebase will replay 'their' commits on the new 'our' B branch:

x--x..x..x..x <- old "theirs" commits, now "ghosts", available through reflogs     \      \       \--y--y--y--x'--x'--x'(*) <-  branch B with HEAD updated ("ours")                ^                |         upstream branch 

Note: the "upstream" notion is the referential set of data (a all repo or, like here, a branch, which can be a local branch) from which data are read or to which new data are added/created.

'local' and 'remote' vs. 'mine' and 'theirs'

Pandawood adds in the comments:

For me, the question still remains, which is "local" and who is "remote" (since the terms "ours" and "theirs" are not used when rebasing in git, referring to them just seems to make an answer more confusing).

GUI git mergetool

kutschkem adds, and rightly so:

When resolving conflicts, git will say something like:

local: modified file and remote: modified file.  

I am quite sure the question aims at the definition of local and remote at this point. At that point, it seems to me from my experience that:

  • local references the partially rebased commits: "ours" (the upstream branch)
  • remote refers to the incoming changes: "theirs" - the current branch before the rebase.

git mergetool does indeed mention 'local' and 'remote':

Merging: f.txt  Normal merge conflict for 'f.txt':   {local}: modified file   {remote}: modified file Hit return to start merge resolution tool (kdiff3): 

For instance, KDiff3 would display the merge resolution like so:


And meld would display it too:

Meld diff

Same for VimDiff, which displays:

Invoke Vimdiff as a mergetool with git mergetool -t gvimdiff. Recent versions of Git invoke Vimdiff with the following window layout:

+--------------------------------+ | LOCAL  |     BASE     | REMOTE | +--------------------------------+ |             MERGED             | +--------------------------------+ 
  • LOCAL:
    A temporary file containing the contents of the file on the current branch.
  • BASE:
    A temporary file containing the common base for the merge.
    A temporary file containing the contents of the file to be merged.
    The file containing the conflict markers.

Git has performed as much automatic conflict resolution as possible and the state of this file is a combination of both LOCAL and REMOTE with conflict markers surrounding anything that Git could not resolve itself.
The mergetool should write the result of the resolution to this file.

vote vote


The bottom line

git rebase

  • LOCAL = the base you're rebasing onto
  • REMOTE = the commits you're moving up on top

git merge

  • LOCAL = the original branch you're merging into
  • REMOTE = the other branch whose commits you're merging in

In other words, LOCAL is always the original, and REMOTE is always the guy whose commits weren't there before, because they're being merged in or rebased on top

Prove it!

Certainly. Don't take my word for it! Here's an easy experiment you can do to see for yourself.

First, make sure you have git mergetool configured properly. (If you didn't, you probably wouldn't be reading this question anyway.) Then find a directory to work in.

Set up your repository:

md LocalRemoteTest cd LocalRemoteTest 

Create an initial commit (with an empty file):

git init notepad file.txt  (use the text editor of your choice)   (save the file as an empty file) git add -A git commit -m "Initial commit." 

Create a commit on a branch that isn't master:

git checkout -b notmaster notepad file.txt   (add the text: notmaster)   (save and exit) git commit -a -m "Add notmaster text." 

Create a commit on the master branch:

git checkout master notepad file.txt   (add the text: master)   (save and exit) git commit -a -m "Add master text."  gitk --all 

At this point your repository should look like this:

Repository with a base commit and two one-commit branches

Now for the rebase test:

git checkout notmaster git rebase master   (you'll get a conflict message) git mergetool   LOCAL: master   REMOTE: notmaster 

Now the merge test. Close your mergetool without saving any changes, and then cancel the rebase:

git rebase --abort 


git checkout master git merge notmaster git mergetool   LOCAL: master   REMOTE: notmaster git reset --hard  (cancels the merge) 

Your results should be the same as what's shown up top.

vote vote


I didn't get your problem exactly but I think the following diagram resolves your issue. (Rebase : Remote Repository ---> Workspace)

Source: My Git Workflow

vote vote


I too, was confused for a long time, often making the wrong decision and had to start over.

DISCLAIMER: I'm no git expert so if anything here is wrong, please correct me!

I think I have come to the realization that my confusion was because I pictured a rebase differently than what many draw it. Here are two drawings that commonly are used to describe a rebase:

   --1--2--3--4--5      \       6--7--8 

and then


And of course that is one way to draw it, but my feeling of what is happening with a rebase is this:

   --1--2--3--4--5                  \                   6--7--8 

Which of course is exactly the same. But from an "ours/theirs" perspective it is different. In the second case it feels as if "we" are still "on" the branch ("6--7--8") and we want to pick up the changes from the "master". So in this world "ours" is still the "branch". And this is what confused me.

But in the first "world view", which I suppose is Git's view, we move to the master (the commit we want to rebase onto) and from there we pick each of the commits on the branch in turn and apply them. So "ours" becomes the "master", initially 5. After 6 has been applied successfully, "ours" is the 6, but actually the 6' that is "on" the master:

   --1--2--3--4--5--6'      \       6--7--8 

And then we go on for the same with the "7".

So in a merge you "are on" the 8 and combine the two into a new commit, but in a rebase you move to the 5 and try to apply the diffs in the commits on the branch as new commits there.

So the "true" picture of the end result of a rebase should really be:

   --1--2--3--4--5--6'--7'--8'      \       6--7--8 

And after the rebase you are on 8'. And so is you branch (I think!). And this could be visualized (in my mind) as:

   --1--2--3--4--5      \           \       6--7--8     6'--7'--8' 
vote vote


Top 3 video Explaining conflict - git rebase, keeping track of 'local' and 'remote'