Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a quick way to "git diff" from the point or branch origin?

Tags:

git

git-diff

I have looked at various SO answers on using git diff and git revisions (HEAD, ORIG_HEAD, FETCH_HEAD, etc.) and I still haven't found an easy way to list the changes have been made since the beginning of the local branch, or since last rebase.

By easy I mean without having to look up and paste commit SHA or having to count how many commits I want to look back.

git diff origin/master is close, but it refers to remote which may have diverged since I checked out new branch from it.

I would expect something like git diff BASE_HEAD to be available.

...unless there's already a way to do that. Does anyone have the answer?

like image 448
user776686 Avatar asked Apr 22 '15 22:04

user776686


People also ask

How do you find the origin of a diff?

You can git branch -a to list all branches (local and remote) and then choose the branch name from the list (just remove remotes/ from the remote branch name. Example: git diff main origin/main (where "main" is the local main branch and "origin/main" is a remote, namely the origin and main branch.)

What is the command to check diff 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.

What does git diff origin do?

Diff command is used in git to track the difference between the changes made on a file. Since Git is a version control system, tracking changes are something very vital to it. Diff command takes two inputs and reflects the differences between them.

How can you tell the difference between a branch and a master?

git diff origin/master... This shows only the changes between my currently selected local branch and the remote master branch, and ignores all changes in my local branch that came from merge commits. For reference, if you need the commit refs of commits that contain these changes, use git cherry origin/master .


2 Answers

You can find the branch point using git merge-base. Consider master the mainline and dev the branch whose history you are interested in. To find the point at which dev was branched from master, run:

git merge-base --fork-point master dev 

We can now diff dev against this basis:

git diff $(git merge-base --fork-point master dev)..dev 

If dev is the current branch this simplifies to:

git diff $(git merge-base --fork-point master) 

For more information see the git-merge-base documentation.

like image 180
frasertweedale Avatar answered Sep 18 '22 15:09

frasertweedale


Use git diff @{u}...HEAD, with three dots.

With two dots, or with HEAD omitted, it will show diffs from changes on both sides.

With three dots, it will only show diffs from changes on your side.

Edit: for people with slightly different needs, you might be interested in git merge-base (note that it has plenty more options than the other answer uses).

like image 35
o11c Avatar answered Sep 19 '22 15:09

o11c