git: diff between file in local repo and origin

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

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, however I just want to isolate it down to this one particular file.

For simplicity lets say the files is named file1.txt and it has a local file path = [local_path] and in the origin it has filepath = [remote-path].

What would be the git command I need to type?

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.

回答1:

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



回答2:

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.



回答3:

for that wrote an bash script

#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 

in above script i fetch remote main branch (not necessary its master branch ANY branch )to FETCH_HEAD them make a list of my modified file only and compare modified files to git difftool

here many difftool supported by git, i configure 'Meld Diff Viewer' for good GUI comparison . From 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 main branch.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!