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 localmaingit diff origin/main: compare against your local remote-tracking ref fororigin/main
If your local main is outdated, then git diff main can be misleading.
For example:
- maybe you have not switched to
mainfor a week, and other developers have merge to main meanwhile. - maybe you did not run
git pullonmain - but
git fetchalready updatedorigin/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/maincompares your current work against the current tip oforigin/maingit diff origin/main...compares your current work against the merge base of your branch andorigin/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.