In Git, how do I diff to the first commit of my branch?
I've made several commits to a local branch, but I'm not sure what the best way to diff what I currently have to my branch's starting state. I know I can do something like git diff HEAD HEAD~6
if there's been 6 commits to my branch, but I was hoping for something that was independent of the number of commits.
Edit: I failed to mention this: I was hoping I wouldn't have to dig through the log to get the hash of the commit I branched from. If I had 80 commits for example, this wouldn't be a fun task.
Also, presume that the original branch I branched from has already had several changes.
You'll want to use the triple dot syntax described in git help diff
:
git diff otherbranch...
This is the same as:
git diff otherbranch...HEAD
which is the same as:
git diff $(git merge-base otherbranch HEAD) HEAD
The merge-base
command prints the "best" (most recent) common ancestor, so the above command shows the difference from the most recent commit that HEAD
has in common with otherbranch
to HEAD
.
Note you can use @{u}
in place of otherbranch
to see the changes you made since you diverged from the upstream branch. See git help revisions
for details about the syntax.