86

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Terry
  • 14,099
  • 9
  • 56
  • 84
  • 1
    You can do a git diff without sending to a text file. Viewing in the console is generally efficient in my opinion. – Denys Séguret Aug 13 '12 at 15:21
  • @dystroy I know, but my problem was Windows vs. Unix LFs, so entire files were being tracked as diff'd. I found the console inefficient for such large output. – Terry Aug 13 '12 at 15:23
  • What tool do you use on windows ? I think the standard parameterization of msysgit handles this automatically. – Denys Séguret Aug 13 '12 at 15:24
  • Oh, and ST2 color codes the output, which is very handy. – Terry Aug 13 '12 at 15:24
  • I used the new [GitHub for Windows](http://windows.github.com/) for the original commit/push and GitHub's web interface for the original pull request. – Terry Aug 13 '12 at 15:26
  • I won't add more on this topic because I never used this tool but there probably is a solution for automatic conversion of end of lines. I know I don't have problems (using msysgit) even while my colleagues are on windows and I'm on linux. Maybe you could ask a separate question for this so you don't have to sort your diff ? – Denys Séguret Aug 13 '12 at 15:29
  • possible duplicate of [compare local git branch with remote branch?](http://stackoverflow.com/questions/1800783/compare-local-git-branch-with-remote-branch) – nawfal Feb 07 '14 at 01:42
  • Or simply do `git diff /` – pavanlimo Feb 24 '15 at 00:36
  • @Terry FYI: there's some decent docs on dealing with line endings across windows/unix: https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_code_core_autocrlf_code and https://help.github.com/articles/dealing-with-line-endings/ – DylanYoung Oct 25 '18 at 19:27

7 Answers7

181

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.

HieroB
  • 3,917
  • 4
  • 17
  • 22
  • 6
    Upvoting since it provides more detail than other answers. – paxdiablo Jan 22 '16 at 05:05
  • 7
    Upvoted for the detail and explanations of context. If someone is asking this question they are probably starting out with git. Context and clear explanations are important when you're trying to wrap your head around a new technology, and it certainly never hurts. – Michael Harris Mar 04 '16 at 16:22
  • 8
    I'd add: 2b. `git diff --stat FETCH_HEAD` to provide a bit more info if `--summary` wasn't enough. – Randall Mar 31 '16 at 14:26
  • 2
    If I want to avoid merge conflicts before i push my changes. Is it best to do a fetch and then do a diff of my local working directory with the FETCH_HEAD and then do a push if everything is okay? – Nuetrino Jan 04 '18 at 13:28
  • What if after doing the comparison I decide I do not want to merge my local branch with the remote one; is there a way to "unfetch" the remote branch? – Pedro García Medina Jan 22 '21 at 20:54
  • Pedro: Fetch writes into the file .git/FETCH_HEAD, and you reference the contents via FETCH_HEAD. If you don't merge them, they will not change your local branch. The contents of .git/FETCH_HEAD are overwritten with the next fetch, so usually you can just leave it. I don't know of a git command to explicitly clear FETCH_HEAD. – HieroB Jan 23 '21 at 22:15
47

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
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
6

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gzh
  • 3,507
  • 2
  • 19
  • 23
3

You can use: git diff remote/my_topic_branch my_topic_branch

Where my_topic_branch is your topic branch.

Elisha Senoo
  • 3,489
  • 2
  • 22
  • 29
  • 1
    this gives me error: fatal: ambiguous argument 'origin/': unknown revision or path not in the working tree. – BobHy Apr 09 '23 at 04:02
1

EG git diff master origin/master

Tuntable
  • 3,276
  • 1
  • 21
  • 26
0

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.

0

I just had a related problem where I wanted to get a quick overview of changes in a GitHub PR before merging it.

Basically I was only interested in knowing how many files were added, modified, or deleted in that PR. The PR had thousands of changes, which made the GitHub web UI useless.

If anyone has a similar problem, try doing this on your local machine:

  1. git fetch origin branch as @HieroB suggested. This will get metadata about the latest changes in the remote branch without pulling all these changes locally.

  2. git diff --name-status FETCH_HEAD > difflog.txt. Git will create a list of all files that were changed in the remote branch, and save the list to a file. The structure of the resulting file will be like this:

    M  files/file1.json
    D  files/file2.json
    A  files/file3.json
    

    where the change marker M stands for "modified", D stands for "deleted", and A stands for "added. In each line, its change marker will be delimited from the file name with a tab character.

  3. Open the file in a text editor, copy each of the 3 change markers along with their adjacent tab characters, and use your text editor's Find in file functionality to search for the marker's occurrences.

Jura Gorohovsky
  • 9,886
  • 40
  • 46