Home > OS >  Confusion about Git command: git diff origin <branch> HEAD
Confusion about Git command: git diff origin <branch> HEAD

Time:01-25

1 to 2 weeks ago, I found a reference on the web, saying that this command:

$ git  diff  origin  <branch>  HEAD

will show the present (local) commits that have not yet been pushed to remote. Since then, I have figured out that, as long as I am in the specified branch of interest, I do not need to specify the branch name on that command line.

I've been using that command, ever since, to help me perform some experiments, to help me learn how Git/GitLab work. Staying only in the development branch and doing no merges, I have repeatedly been able to:

  1. use that command to display not-yet-pushed commits; and then, after doing the "push"
  2. to run that command again and notice that all the previous commits, in the output of that command, were completely gone - i.e., the command gave no output.

But now I am experiencing some inconsistencies in the output of that command:

  1. I created a new branch, off of master, called jravery.
  2. I then created a new sub-branch, off of jravery, called jrasub.
  3. I added some files to jrasub; then staged & committed & pushed them.
  4. Then I merged jrasub into jravery; then I did a git push, in the jravery branch.

But now I do the git diff origin {branch} HEAD command and I still see those 2 files, whether the specified branch is jravery or jrasub.

Why were the committed-and-then-pushed files/commits disappearing from the output of that diff command last week, after push, but now still appearing in the output of that same command this week (also after push)?

Also in the output of that command - whether I specify master or jravery or jrasub as the branch - I am seeing a few other files that were never included in either the jravery or the jrasub branch. However, when I specify the development branch, nothing shows up in the output of that command, even though most of the files that are otherwise showing up in this command's output are - or were - only in the development branch (unless and until they might eventually have been merged into master) and never were part of either jravery or jrasub.

Why is that?

CodePudding user response:

The problem is that git diff compares the first 2 arguments. The argument "origin" is just another commit, which in a normal bare repo likely just points to the default branch. Run this command to see what it is:

git log origin

For me in a repo that uses Git Flow, my top commit from that command yields the commit at: origin/develop, origin/HEAD.

So basically, this entire time you may not have been comparing what you thought you were comparing. If you change your command to:

git diff origin/<branch-name> HEAD

you'll probably get what you want.

Tip: there's an easier way to do this without having to type in the branch name at all:

git diff @{u} @

Basically, in this context, @ is short for HEAD, and @{u} is shorthand for your currently checked out branch's remote tracking branch (e.g. origin/my-branch-name). Since it's branch agnostic this'll work on any branch you currently have checked out, compared to it's remote tracking branch.

Side Note: git diff with two arguments is the same thing as using two dots to connect the arguments: git diff A B equals git diff A..B. But note if your branches have diverged you'll get both sides of the diff. If you only want one side, you can use 3 dots: git diff A...B. More info here.

CodePudding user response:

@ObsidianAge provided the best answer, as far as I can tell. Thanks.

  •  Tags:  
  • Related