Git Tips Slides
PageUp x2: previous · PageDown x2: next · Home: index

git diff main vs git diff origin/main

If you work on a branch that was created from main, you might want to compare your current work against main. Similar to the view you see in web UIs like GitHub.

flowchart LR remote["origin (Git hosting platform)"] tracking["your local origin/main"] localmain["your local main"] remote -- "git fetch updates this" --> tracking tracking -. "local main may lag behind" .-> localmain

These two commands can show different results:

  • git diff main: compare against your local main
  • git diff origin/main: compare against your local remote-tracking ref for origin/main

If your local main is outdated, then git diff main can be misleading.

For example:

  • maybe you have not switched to main for a week, and other developers have merge to main meanwhile.
  • maybe you did not run git pull on main
  • but git fetch already updated origin/main

If you want to compare against the current state of the remote main branch, use this sequence:

git fetch
git diff origin/main

But note: git diff origin/main is still not the same as "show me only the changes from my branch since it was created".

For that PR-like view, use the merge-base form:

git fetch
git diff origin/main...

Why is this different?

  • git diff origin/main compares your current work against the current tip of origin/main
  • git diff origin/main... compares your current work against the merge base of your branch and origin/main

That is why git diff origin/main... is usually closer to what you see in a pull request: it shows the changes on your branch since it diverged from origin/main.