For eg – Lets say you have Branch A
and Branch B
that have the same content since Branch B was branched off from Branch A.
Now, lets say we rebase master into Branch B – git rebase master
If I do a git diff locally now: git diff branchA..branchB
The result: No changes detected
However, If I open a pull request on github with source as BranchB and target as BranchA.
The same content that’s same between the two branches will be shown in the diff since the commit hashes are different because rebasing re-writes the commit.
So just to confirm what I am seeing, github’s diffing is different from git’s diffing?
1
Why GitHub’s Diffing is Different from Git’s Diffing
Yes, GitHub’s diffing behaves differently from git diff
because GitHub takes into account the entire commit history of the branches, in addition to their content. Here’s an explanation:
1. git diff
Locally
When you run git diff branchA..branchB
, Git compares the content of the files in the tip (latest commit) of branchA
and branchB
.
- If the content of the files is identical (which is true in your case), it will report no differences, even if the commit histories of the branches are different.
- Git’s
diff
focuses solely on the current state of the files and ignores commit history.
2. GitHub’s Diff in Pull Requests
GitHub generates a diff for a pull request by comparing the commit histories of the source (branchB
) and target (branchA
).
- It calculates the changes introduced by the commits in
branchB
that are not inbranchA
. - Since rebasing rewrites commit history (creating new commit hashes), GitHub sees these as new commits, even if their content is identical.
Why GitHub Shows Differences
When you rebase branchB
onto master
, it rewrites branchB
‘s history.
- Even though the file content in
branchB
andbranchA
is identical, GitHub sees the rewritten commits inbranchB
as “new” relative tobranchA
. - As a result, the pull request shows a diff because the commits in
branchB
are not part ofbranchA
‘s history anymore.
Key Takeaways
- Git’s
diff
compares file content between commits and ignores history. - GitHub’s pull request diff considers the commit history and treats rewritten commits as new, even if their content matches.
How to Avoid Confusion
- Use merge-based workflows instead of rebases to preserve the commit history.
- Use GitHub’s
--squash
merge option to collapse the rewritten history into a single commit, minimizing discrepancies in commit histories.