git:本地存储库中的文件和源文件之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21101572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git: diff between file in local repo and origin
提问by SeekingAlpha
I want to find the differences between a file I have in my local repo vs what is in the origin master
.
我想找到本地存储库中的文件与origin master
.
I know that there is git diff
, however I just want to isolate it down to this one particular file.
我知道有git diff
,但是我只想将它隔离到这个特定文件。
For simplicity lets say the file is named file1.txt
and it has a local file path = [local_path]
and in the origin it has filepath = [remote-path]
.
为简单起见,假设文件已命名file1.txt
并且它有一个本地文件路径 =[local_path]
并且在原点它有 filepath = [remote-path]
。
What would be the git command I need to type?
我需要输入的 git 命令是什么?
EDIT: Thank you all for your input it has been very insightful. For those that are using Eclipse (which I am and I should have stated earlier) I just found out that you can just rightclick -> Compare With -> Branch, Tag or Reference -> select appropriate version and there you go.
编辑:感谢大家的投入,非常有见地。对于那些使用 Eclipse 的人(我应该早点说明),我刚刚发现您只需右键单击 -> 比较 -> 分支、标记或引用 -> 选择适当的版本,然后就可以了。
回答by Code-Apprentice
If [remote-path]
and [local-path]
are the same, you can do
如果[remote-path]
和[local-path]
相同,你可以做
$ 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
注 1:上面的第二个命令将与本地存储的远程跟踪分支进行比较。需要 fetch 命令来更新远程跟踪分支以与远程服务器的内容同步。或者,你可以做
$ git diff master:<path-or-file-name>
Note 2:master
can be replaced in the above examples with any branch name
注2:master
可以在上面的例子中用任何分支名称替换
回答by janos
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.
像往常一样,ref1
和ref2
可能分支名称,远程名称/分支名称,提交的SHA等。
回答by Ratna Halder
To compare local repository with remote one, simply use the below syntax:
要将本地存储库与远程存储库进行比较,只需使用以下语法:
git diff @{upstream}
回答by adg
For that I wrote a bash script:
为此,我编写了一个 bash 脚本:
#set -x
branchname=`git branch | grep -F '*' | awk '{print }'`
echo $branchname
git fetch origin ${branchname}
for file in `git status | awk '{if ( == "modified:") print ;}'`
do
echo "PLEASE CHECK OUT GIT DIFF FOR "$file
git difftool FETCH_HEAD $file ;
done
In the above script, I fetch the remote main branch (not necessary its master branch ANY branch) to FETCH_HEAD
, then make a list of my modified file only and compare modified files to git difftool
.
在上面的脚本中,我将远程主分支(不需要它的主分支任何分支)获取到FETCH_HEAD
,然后仅列出我修改过的文件并将修改后的文件与git difftool
.
There are many difftool
supported by git, I configured Meld Diff Viewer
for good GUI comparison.
From the above script, I have prior knowledge what changes done by other teams in same file, before I follow git stages untrack-->staged-->commit
which help me to avoid unnecessary resolve merge conflict with remote team or make new local branch and compare and merge on the main branch.
difftool
git 支持的有很多,我是Meld Diff Viewer
为了比较好的 GUI配置的。
从上面的脚本中,在我遵循 git 阶段之前,我已经知道其他团队在同一个文件中做了哪些更改,untrack-->staged-->commit
这有助于我避免不必要的解决与远程团队的合并冲突或创建新的本地分支并在主分支上进行比较和合并。
回答by animati
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....
然后你确实比较了本地 git 和远程 git 的最后提交数....