Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I compare the commits in one Git branch that are not in another?

Quick answer

To list commits reachable from topic but not from main, run git log main..topic --oneline. The range direction matters: it lists commits on the branch named second that are not reachable from the branch named first. Reverse the names to inspect commits on main that topic lacks. Use git diff when you want to compare file contents instead; it answers a different question. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have two local branches, main and topic. How do I see which commits are on topic but not on main?

Geez

Run git log main..topic --oneline. Read the range as “commits reachable from topic, excluding commits reachable from main”: the branch after the two dots is the side being listed. [S1]

For example, if the output is:

f3a21c4 Add export option 8bc910d Handle empty input

then those are example commits listed for topic and not reachable from main. This output is illustrative, not a prediction of what your repository contains. The command lists history; it does not change either branch. If you want the default, fuller log display rather than compact one-line entries, omit --oneline.

Follow-up question

Does that compare the files too? I mainly want to know what code differs.

Geez

Not directly. git log main..topic --oneline lists commits unique to the second side of the range. To compare the endpoint file contents instead, use git diff main topic. Those answer different questions: branches can have different histories yet end up with identical file contents. [S1] [S2]

If you want to inspect changes introduced on topic relative to the branches’ common history, git diff main...topic is another option: it compares topic with the merge base of main and topic. That is still a file-content comparison, not a list of unique commits. [S2] Choose the command based on whether you want a history list or a comparison of file contents.

Follow-up question

How can I check what main has that topic doesn’t, and avoid reversing the result by mistake?

Geez

Reverse the names: git log topic..main --oneline lists commits reachable from main but not from topic. [S1]

A useful check before running either command is to say the range aloud: git log A..B means “show me B’s commits that A does not have.” The first branch is the exclusion side; the second branch is the side being listed. If the result is empty, this command reports no commits on the second side that are absent from the first; it does not mean that the two branches’ files are necessarily identical. Keep the branch order tied to the question: put the branch whose commits you want to see second.

Follow-up question

Can you give me a short repeatable check for both directions?

Geez

Replace the example names with your branch names, then run both history listings:

git log main..topic --oneline git log topic..main --oneline

The first checks commits on topic not reachable from main; the second checks commits on main not reachable from topic. [S1] If you also need to compare the current file contents, separately run git diff main topic. [S1] [S2]

Keep the questions separate when reporting results: “Which commits are unique to this branch?” calls for git log A..B; “How do the endpoint contents differ?” calls for git diff A B. The commands show different views, so neither output should be treated as a substitute for the other.

A practical checklist

  • Name the two branches you want to compare.

  • Put the branch whose commits you want listed second in git log A..B.

  • Use --oneline for a compact commit list, or omit it for the default log display.

  • Reverse the branch names to inspect the other direction.

  • Use git diff A B for endpoint file-content differences, not for a unique-commit list.

A few more useful answers

What does an empty `git log main..topic --oneline` result tell me?

It means the command found no commits reachable from topic that are absent from main. It does not by itself establish that the branches’ file contents are identical.

Should I use `git diff main...topic` to list commits unique to `topic`?

No. The three-dot form compares file contents between topic and the merge base of the branches. Use git log main..topic to list commits on topic that main does not contain. [S1] [S2]

How do I see commits unique to both branches?

Run git log main..topic --oneline and git log topic..main --oneline. Each range lists commits from its second branch that are not reachable from its first. [S1]

Sources

References used for this guide.

  1. [S1] Git git-log documentation: commit ranges
    git-scm.com · Checked
  2. [S2] Git git-diff documentation: comparing commit endpoints
    git-scm.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work