【问题标题】:Difference between file in local repository and origin本地存储库中的文件与源文件之间的区别
【发布时间】:2014-02-01 19:20:25
【问题描述】:

我想找出本地存储库中的文件与 origin master 中的文件之间的差异。

我知道有git diff。但是,我只想将其隔离到这个特定的文件中。

为简单起见,假设文件名为 file1.txt,它的本地文件路径 = [local_path],在源文件路径 = [remote-path]

我需要输入什么 Git 命令?


对于那些使用Eclipse 的人,我刚刚发现您只需右键单击→ 比较 → 分支、标签或参考 → 选择合适的版本就这样。

【问题讨论】:

  • [remote-path] 与 [local-path] 不同吗?
  • 什么叫“原点大师”?
  • eclipse egit方法不错

标签: git git-diff


【解决方案1】:

我尝试了几个解决方案,但我认为一个简单的方法是这样的(你在本地文件夹中):

#!/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

运行此脚本后,您将完成使用上次提交号比较本地 Git 和远程 Git。

【讨论】:

    【解决方案2】:

    检查当前分支:

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

    要检查其他分支,请说 staging:

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

    【讨论】:

    • "staging" 就像在 Git staging 中一样?还是字面上的“分期”?或者他们是同一个东西?你能详细说明吗?请通过editing your answer 回复,而不是在 cmets 中(without "Edit:"、"Update:" 或类似的 - 答案应该看起来像是今天写的)。
    【解决方案3】:

    关于本地和远程可能存在不同路径的原始问题的完整答案如下:

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

    假设本地路径是docs/file1.txt,远程路径是docs2/file1.txt,使用git diff master -- docs/file1.txt origin/master -- docs2/file1.txt

    本文改编自GitHub帮助页面here和之前的Code-Apprentice answer

    【讨论】:

      【解决方案4】:

      要将本地存储库与远程存储库进行比较,只需使用以下语法:

      git diff @{upstream}
      

      【讨论】:

        【解决方案5】:

        为此,我编写了一个 Bash 脚本:

        #set -x
        branchname=`git branch | grep -F '*' |  awk '{print $2}'`
        echo $branchname
        git fetch origin ${branchname}
        for file in `git status | awk '{if ($1 == "modified:") print $2;}'`
        do
        echo "PLEASE CHECK OUT GIT DIFF FOR "$file
        git difftool  FETCH_HEAD $file ;
        done
        

        在上面的脚本中,我将远程主分支(不一定是它的主分支 - any 分支)获取到FETCH_HEAD,仅列出我修改过的文件,并将修改后的文件与git difftool.

        Git 支持的difftool 有很多,我配置了 Meld Diff Viewer 以获得良好的 GUI 比较。

        从上面的脚本中,在我遵循 Git 阶段 untrackedstagedcommit 之前,我已经知道其他团队在同一文件中做了哪些更改,这有助于我避免与远程团队不必要的解决合并冲突或创建新的本地分支并在主分支上进行比较和合并。

        【讨论】:

          【解决方案6】:

          如果[remote-path][local-path] 相同,可以这样做

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

          注意 1: 上面的第二个命令将与本地存储的远程跟踪分支进行比较。需要 fetch 命令来更新远程跟踪分支以与远程服务器的内容同步。或者,您可以这样做

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

          注意 2:master 可以在上面的例子中替换为任何分支名称

          【讨论】:

          • 如果是当前目录,甚至可以省略本地路径。
          • 对于像我这样的菜鸟,这里有一个例子:git diff master:README.md -- README.md
          • 其实这里有一个更好的例子:git diff origin/master -- README.md
          • 我不能做git fetch master,而是使用git fetch .(origin/master 也不起作用)但其余的都很好。
          • @rob 你是第一个指出我的错误的人。应该是git fetch **origin**
          【解决方案7】:

          查看从远程文件到本地文件的差异:

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

          查看另一个方向的差异:

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

          基本上你可以在任何地方使用这个符号来区分任何两个文件:

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

          像往常一样,ref1ref2 可以是分支名称、远程名称/分支名称、提交 SHA 等。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-08-07
            • 2012-03-11
            • 1970-01-01
            • 2017-09-20
            • 2021-05-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多