'git diff' between a remote and local repository

GitGithubGit Diff

Git Problem Overview


I am trying to diff my local file with a GitHub repository before I submit a pull request, so I can see what will show up. Is there an accurate way of doing this?

I assume GitHub's compare tool manipulates Git's diff.

Git Solutions


Solution 1 - Git

To compare a local working directory against a remote branch, for example origin/master:

  1. git fetch origin master
    This tells git to fetch the branch named 'master' from the remote named 'origin'. git fetch will not affect the files in your working directory; it does not try to merge changes like git pull does.
  2. git diff --summary FETCH_HEAD
    When the remote branch is fetched, it can be referenced locally via FETCH_HEAD. The command above tells git to diff the working directory files against FETCHed branch's HEAD and report the results in summary format. Summary format gives an overview of the changes, usually a good way to start. If you want a bit more info, use --stat instead of --summary.
  3. git diff FETCH_HEAD -- mydir/myfile.js
    If you want to see changes to a specific file, for example myfile.js, skip the --summary option and reference the file you want (or tree).

As noted, origin references the remote repository and master references the branch within that repo. By default, git uses the name origin for a remote, so if you do git clone <url> it will by default call that remote origin. Use git remote -v to see what origin points to.

You may have more than one remote. For example, if you "fork" a project on GitHub, you typically need a remote referencing the original project as well as your own fork. Say you create https://github.com/yourusername/someproject as a fork of https://github.com/theoriginal/someproject. By convention, you would name the remote to the original repo upstream, while your own fork would be origin. If you make changes to your fork on GitHub and want to fetch those changes locally, you would use git fetch origin master. If the upstream has made changes that you need to sync locally before making more changes, you would use git fetch upstream master.

Solution 2 - Git

Don't do a pull :

  • do a fetch (the syntax is the same as git pull, but it doesn't automatically merge)
  • do a diff between your dest branch and the other branch
  • then do a merge if you want

Solution 3 - Git

Per the OP's comment that part of his "problem was Windows vs. Unix LFs", this should help:

You can use the following configuration command to tell git-diff to ignore the difference of the EOL code.

git config --global core.whitespace cr-at-eol

Solution 4 - Git

You can use: git diff remote/my_topic_branch my_topic_branch

Where my_topic_branch is your topic branch.

Solution 5 - Git

EG git diff master origin/master

Solution 6 - Git

Important to remember: Git diff will show you diferences between two commited branchs ( remote or local).

Step 1) - Commit local

So the first step to do is make sure you have commited your local repository. You can run git status to see if there is any file left.

If you need to add a file run a git add {filename} or to add all files git add .. Then you can run a git commit -m "message" to commit you local files.

Step 2) - Fetch your branch from remote

You can fetch your remote using git fetch origin master. If you see a message like below, you are good to go.

branch            master     -> FETCH_HEAD

> Note that origin is the repository and master is the branch within > that repository.

Step 3 Check differences

Now you can either git diff --summary FETCH_HEAD or git diff origin/master master --summary too see your changes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTerryView Question on Stackoverflow
Solution 1 - GitHieroBView Answer on Stackoverflow
Solution 2 - GitDenys SéguretView Answer on Stackoverflow
Solution 3 - GitgzhView Answer on Stackoverflow
Solution 4 - GitElisha SenooView Answer on Stackoverflow
Solution 5 - GitTuntableView Answer on Stackoverflow
Solution 6 - GitEwerton XavierView Answer on Stackoverflow