Coliban
Coliban

Reputation: 671

How do I show differences between local and remote files in Git?

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 [email protected]:/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?

Upvotes: 9

Views: 41293

Answers (3)

Rahul Bhat
Rahul Bhat

Reputation: 327

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.

Upvotes: 9

Daniel Trugman
Daniel Trugman

Reputation: 8491

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/*

Upvotes: 7

user4691348
user4691348

Reputation:

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.

Upvotes: 4

Related Questions