git: diff between file in local repo and origin

后端 未结 7 2122
我在风中等你
我在风中等你 2020-12-04 04:42

I want to find the differences between a file I have in my local repo vs what is in the origin master.

I know that there is git diff, howev

相关标签:
7条回答
  • 2020-12-04 05:05

    To compare local repository with remote one, simply use the below syntax:

    git diff @{upstream}
    
    0 讨论(0)
  • 2020-12-04 05:13

    Full answer to the original question that was talking about a possible different path on local and remote is below

    1. git fetch origin
    2. git diff master -- [local-path] origin/master -- [remote-path]

    Assuming the local path is docs/file1.txt and remote path is docs2/file1.txt, use git diff master -- docs/file1.txt origin/master -- docs2/file1.txt

    This is adapted from GitHub help page here and Code-Apprentice answer above

    0 讨论(0)
  • 2020-12-04 05:17

    If [remote-path] and [local-path] are the same, you can do

    $ git fetch origin master
    $ git diff origin/master -- [local-path]
    

    Note 1: The second command above will compare against the locally stored remote tracking branch. The fetch command is required to update the remote tracking branch to be in sync with the contents of the remote server. Alternatively, you can just do

    $ git diff master:<path-or-file-name>
    

    Note 2: master can be replaced in the above examples with any branch name

    0 讨论(0)
  • 2020-12-04 05:21

    To view the differences going from the remote file to the local file:

    git diff remotename/branchname:remote/path/file1.txt local/path/file1.txt
    

    To view the differences in the other direction:

    git diff HEAD:local/path/file1.txt remotename/branchname:remote/path/file1.txt
    

    Basically you can diff any two files anywhere using this notation:

    git diff ref1:path/to/file1 ref2:path/to/file2
    

    As usual, ref1 and ref2 could be branch names, remotename/branchname, commit SHAs, etc.

    0 讨论(0)
  • 2020-12-04 05:23

    To check with current branch:

    git diff -- projects/components/some.component.ts ... origin
    git diff -- projects/components/some.component.html ... origin
    

    To check with some other branch say staging:

    git diff -- projects/components/some.component.ts ... origin/staging
    git diff -- projects/components/some.component.html ... origin/staging
    
    0 讨论(0)
  • 2020-12-04 05:24

    I tried a couple of solution but I thing easy way like this (you are in the local folder):

    #!/bin/bash
    git fetch
    
    var_local=`cat .git/refs/heads/master`
    var_remote=`git log origin/master -1 | head -n1 | cut -d" " -f2`
    
    if [ "$var_remote" = "$var_local" ]; then
        echo "Strings are equal." #1
    else
        echo "Strings are not equal." #0 if you want
    fi
    

    Then you did compare local git and remote git last commit number....

    0 讨论(0)
提交回复
热议问题