Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Git, how do I diff to the first commit of my branch?

Tags:

git

git-diff

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.

like image 875
Newtang Avatar asked Jun 17 '11 19:06

Newtang


People also ask

How do you diff commits in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch. Order does matter when you're comparing branches.

How do I see diff before commit?

The diff can be done with git diff (followed by the filename or nothing if you want to see the diff of all modified files). But if you already did something like git add * , you have to undo with git restore --staged . first.

How can I see my first commit in github?

Usage. Go to any particular repo's landing page (e.g. like the one you're on) and click the bookmarklet, which will take you to the first page (initial commit). By default, it tracks the master branch, but if you change the branch on the landing page, it will go to that branch's first commit.

What is git diff command?

Comparing changes with git diff Diffing is a function that takes two input data sets and outputs the changes between them. git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.


1 Answers

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.

like image 63
Richard Hansen Avatar answered Sep 21 '22 21:09

Richard Hansen