8

Since I am new to Git, I have some trouble getting started (Git version 2.12.3).

I created on a server a file (in GitLab) which I can clone to a local directory.

git clone git@gitlab.server.com:/joe/myproject.git

All fine, and I have a local copy of the file. But when I change remote something to the file (from someone else or on the server), then I am not able to track or to see the changes on the local client, even if they are clearly visible on the master. Not with

git diff file1

or

git diff origin/master

or something else.

I have to fetch the file, merge it, and then I am able to see the newly edited content. Is it possible to observe and detect the new changes before fetching the new file?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Coliban
  • 601
  • 2
  • 9
  • 24
  • 1
    To see the changes that others have made, you would need to `git pull` their changes. Only then can you compare it against the changes you have made. – Lix Oct 17 '17 at 08:47
  • @Lix, since a "pull" contains a "fetch" and a "merge" i would change the local sources, but i would investigate the difference *before* i do such changes. – Coliban Oct 17 '17 at 08:55
  • 2
    you could just `git fetch origin` and then `git diff master..origin/master -- myfile.txt` –  Oct 17 '17 at 08:56
  • @efkin, that is working, than you! – Coliban Oct 17 '17 at 09:06
  • @Coliban, i formulated the comment in terms of answer so other people can benefit. i guess also DanielTrugman answer is more than adequate. feel free to accept one of those please. –  Oct 17 '17 at 09:10

3 Answers3

8

I would do it like this:

git fetch origin master

And then I would do:

git diff master origin/master

In a more simplified way, to see the difference in your local and remote repository, the syntax would be like this:

git diff <masterbranch> <remotebranch>

There is also git difftool command which shows the difference in a UI environment if you have installed one, I have rarely used this, so it's kind of an uncharted area for me. Have fun.

Ribo
  • 3,363
  • 1
  • 29
  • 35
Rahul Bhat
  • 308
  • 2
  • 14
  • 1
    @Mortensen -- I removed the incorrect statement that git pull is equivalent to git fetch and git diff. It is equivalent to git fetch and git merge. – Ribo Dec 16 '19 at 20:36
6

Is it possible to observe and detect the new changes before fetching the new file?

No, if you don't fetch the changes, your local repository is not aware of them.

I have to fetch the file, merge it, and then I am able to see the newly edited content.

You don't have to merge it, only fetch it!

Comparing

If you want to compare a local path against the remote one, use:

git diff <repo>/<branch> -- <path>

For example:

git diff origin/master -- program.cpp
--- OR ---
git diff origin/master -- Utils/*
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Trugman
  • 8,186
  • 20
  • 41
3

In order to accomplish your needs, you need at least to fetch from your remote repository. And only then -and before you merge- you can diff between your branch and your origin/branch.