Stream: git

Topic: git diff on remote branch


view this post on Zulip Dani Coleman (Sep 09 2021 at 15:27):

I want to know if my working directory is up-to-date with the remote branch but I'm getting different results if I use
% git diff origin develop
(17 lines output)
% git diff origin/develop
(9K lines output)

The latter (larger number of diffs) is what I expect. I wonder if there is something wrong with how I'm tracking the remote branch that causes the initial to fail.

% git status
On branch develop
Your branch is up to date with 'origin/develop'.
...
Changes not staged for commit:
modified: data/fieldlist_NCAR.jsonc

[These are the 17 lines different. But I have updated the remote branch on github so would expect the larger number of diffs].

TL;DR

My understanding of this from
https://stackoverflow.com/questions/18137175/in-git-what-is-the-difference-between-origin-master-vs-origin-master
is that my working dir is on the local branch called develop and that local branch is up to date with
the local branch called origin/develop which is supposed to be tracking the
branch named develop on the remote named origin.
( Feel free to comment on this if you see a conceptual error. The fact that there is a local branch called develop and another local called origin/develop which tracks a remote branch is strange to me. Why tthe two?!)

If this is true, I would expect the command:
% git diff origin develop

to return the same result. That is, what is the difference between the code
in the local working directory compared to the branch named develop on the
remote named origin.

However, this returns a huge diff (95325 lines vs 17 in previous case).
It is actually what I expect, as I used github to merge changes from
another remote into my origin develop.

So the question, in case you haven't already identified an error:
is the difference between the results of the two commands an indication
that I've made an error, and/or that my origin/develop is not actually
tracking origin develop ?

% cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git@github.com:bitterbark/MDTF-diagnostics.git
fetch = +refs/heads/:refs/remotes/origin/
[branch "master"]
remote = origin
merge = refs/heads/master
[remote "gfdl"]
url = git@github.com:NOAA-GFDL/MDTF-diagnostics.git
fetch = +refs/heads/:refs/remotes/gfdl/
[branch "develop"]
remote = origin
merge = refs/heads/develop

You are welcome to peer into my directory on CGD machines
/home/bundy/mdtf/old/MDTF-diagnostics-develop-20210701

view this post on Zulip Jared Baker (Sep 09 2021 at 17:17):

git diff origin develop is a diff between origin/HEAD and local develop branch. git diff origin/develop is short for diff between branch 'remotes/origin/develop' and the current working branch. If you want to explicitly look at differences between your local develop branch and the remote origin develop branch, git diff origin/develop develop (at least in my preferred ordering).

Now just because you have a tracking branch in git doesn't mean that it's automatically updated and synced with your local copy of the tracked branch. For example, you can checkout a remote branch with tracking and make changes and your local branch could be several commits ahead of the remote branch being tracked (or vice versa). Does this help clear anything up? You might be interested in what git branch -vv returns on your machine. A tracking branch is nothing more than a relationship that when a push/pull is executed, it sends it to the remotes, but still requires the push to update the remote or pull to update from the remote.

So all in all, the way I read this is that you have 17 lines different from your local development branch and origin/HEAD, but that there are 9K lines difference between the remote origin/develop branch and your current working develop branch.

view this post on Zulip Dani Coleman (Sep 09 2021 at 21:22):

Thanks @Jared Baker , this is very helpful. I can see I've been missing some things conceptually.

When you say

git diff origin/develop
is short for diff between branch 'remotes/origin/develop' and the current working branch.

did you really mean the current working branch rather than the current working directory which is what I would have assumed? Assuming you wrote correctly, do you consider those equivalent? Or are you just assuming that a good git user will have updated their local branch with the changes in their local directory. Is git status the only/right way to see changes between my current working directory and the branch? Is there a way to explicitly reference cwd in the git diff statement to make it use the working directory? (BTW thanks for the example showing what is being diffed against what!)

As for tracking branches, thanks for git branch -vv ; I like that it shows the hashes (name?) since that's the only way I know absolutely where things stand. It does make sense that my local branch can be ahead of a remote branch. I would really like to be able to check if my local directory has the most recent changes from a remote branch, but I think the git diff origin/develop develop will accomplish this. One more question: is there a way to check out a branch without tracking? I kind of thought it always did, unless the head gets detached, which in my experience has happened when I checked out a remote branch without a local one (?).

Thanks again!

view this post on Zulip Jared Baker (Sep 09 2021 at 22:10):

it's whatever the working branch is (directory independent, I try not to talk about directories at all when working with git unless specifying a path to a specific file). git branch would show you the current branch. I'm assuming that you're in a single git repository (working from the command line in whatever directory you're in; also not a submodule). IMO git status is really for a simple check of changes in your working copy for things not yet committed or added. A git diff can take a full path, so it can be interesting. But you can reference any file you want ultimately. It's interesting to play to play with git diff where files may not exist in other branches. So you need to diff similar entities as well. I try to git diff on files with only ones in a working copy with changes rather than cross-referencing branches which I've not checked out. It's just a preference I suppose, but helps me keep things in line I suppose.

As far as making an untracked branch, if you branch out locally and it's not on the remote, then it's an untracked branch. If you already have a branch that is tracked, then git branch --unset_upstream will basically do that.


Last updated: May 16 2025 at 17:14 UTC