0

I want to compare my local repo(after git clone) between remote repo. I use command:

git add newfile
git commit -m "my change"
git diff HEAD origin --name-only

I want to compare only context of changed file. no rename name.

I have two problem: 1. if I change name of file it will show me. 2. if I add/change file in remote repo it will show me. I want to see change relative to my local repo.

How can I solve it ?

1 Answers1

0

If you cloned a branch (for example call it "mybranch") then when you clone the repo you will have a branch called "origin/mybranch". This is effectively the status of the branch on the remote.

Then you checkout the branch locally and you will have a branch called "mybranch" - you do your modifications and then you can do a diff between the two branches:

git diff origin/mybranch mybranch

Or you can simply look at the HEAD as in your example. When you clone the repo you will be at the HEAD. When you commit something you will still be at the HEAD, but the previous commit can be accessed as HEAD~1. So you git diff command can be:

git diff HEAD~1 HEAD

Or just look at the log git log or for a graphical view git log --oneline --graph --all --decorate and pick any two commit-hash's/tags/branches to compare.

Update

I may have misunderstood the question. From your comments I think you are trying to just do a diff for files who's contents have changed. You can use the --diff-filter option:

git diff --diff-filter=M HEAD~1 HEAD

Where "M" is for modified. you can use "R" for renamed as well if you need that (and you can combine many options, e.g. git diff --diff-filter=MR HEAD~1 HEAD for modified and renamed files).

honk
  • 9,137
  • 11
  • 75
  • 83
code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • if I execute git diff from another directory(where no .git). What flag do I need to add ? @code_fodder –  Aug 29 '17 at 11:37
  • If you don't want to just CD into your git directory, you can use the "-C option: `git -C /your/git/dir diff HEAD~1 HEAD` ` – code_fodder Aug 29 '17 at 11:46
  • What is different between my suggest and your ? –  Aug 29 '17 at 11:51
  • Ah ha - updated the answer, I think I did not fully understand your question. I think you wrote "context" instead of "contents"? – code_fodder Aug 29 '17 at 12:08
  • I don't know what different between context and content. I meant that if I change file then it will show me –  Aug 29 '17 at 12:22
  • Ah, no worries... its a bit like this: "Context" means "relevant/relative to". "Contents" refers specifically to what something contains - which I think is what you mean. So a name change could be in the "context" of a file, but does not refer to the "contents" of a file. I think your question is correct, but was not specific enough for me to get exactly what you meant : ) ... anyway, try the filter option that should get you where you need to go. – code_fodder Aug 29 '17 at 13:29
  • Note that lowercase letters such as `diff ... --diff-filter=m` mean *show me **all changes except** those classified as ...*. In this case `M` means "modified", vs e.g. A = added and D = deleted, so this will show you the added and deleted files, but not the modified ones. If you *want* files whose status is M or R, you need `diff-filter=MR`, not `diff-filter=mr`. – torek Aug 29 '17 at 14:32
  • @torek updated - thanks, I think I got that the wrong way around :o – code_fodder Aug 29 '17 at 14:40
  • Where can I find this information ? Git documentation is very difficult understand. –  Aug 29 '17 at 16:02
  • See here: `https://git-scm.com/docs/git-diff` and search for --diff-filter – code_fodder Aug 29 '17 at 16:32
  • I have seen. It is very difficult to understand. it is without examples. –  Aug 29 '17 at 18:19
  • Yeah - its more of a reference I guess, there is one example at the end, but not a real world example. There are so many parameters there to cover... but then you have SO for everything not covered : ) – code_fodder Aug 29 '17 at 19:07
  • Add new file and rename name of file in my local repo are classified as D. why @torek –  Aug 30 '17 at 05:47
  • @Alex: that means you're doing your diff backwards. The arguments to `git diff` are compared in the order you give them, and you get instructions that will change the first commit to become the second commit. Reverse the commit and you get reversed changes. (Rename detection is optional: use `-M` or `--find-renames`, or set `diff.renames` to `true`, or use the latest Git which defaults to having `diff.renames` set to `true`.) – torek Aug 30 '17 at 07:19
  • I don't understand you. if I use --name-status then D. give me full example please. @torek –  Aug 30 '17 at 07:40
  • @Alex take a look here https://stackoverflow.com/questions/7759193/git-diff-renamed-file in this link it shows various methods (with example from the OP). The first two answers are quite detailed and show how you can use "-C", "-M" and "--find-renames" options... – code_fodder Aug 30 '17 at 09:00
  • @Alex: as code_fodder said, see some other examples, but: `git diff HEAD^ HEAD` will show you how to turn your previous commit into your current commit, while `git diff HEAD HEAD^` will show you how to turn your current commit into your previous one. If you added a new file `new.txt`, the *first* diff will say: *to do this, add `new.txt`* and the *second* will say: *to do this, delete `new.txt`*. Until you have rename detection set (with `-M` or one of the other flags), "rename old.name new.name" is represented as "delete contents of old.name, add the same contents as new.name". – torek Aug 30 '17 at 14:19
  • How can I diff only text type files ? I tried add flag -a but it didn't work @torek –  Sep 03 '17 at 09:03
  • @Alex: first you'll have to define what you mean by "text type files". Git's `git diff` can be told to compare specific path specs, or use an "external diff" on specific file paths via `.gitattributes`, but in general `git diff ` means *compare the entire commit*, and to diff only one pair of files you must use `git diff `. – torek Sep 03 '17 at 15:49
  • what difference git diff and git log --pretty="format:" ? HEAD vs FETCH_HEAD ? When do I use git fetch ? Why diff origin/branch branch show all difference since clone although push to remote ?@torek –  Sep 03 '17 at 19:46
  • @Alex: `git log` is for looking at a series of commits, while `git diff` is (mostly) for comparing two specific commits. `git fetch` is for obtaining commits into your own Git repository (where you can use them), from some other Git repository (where you can't). it really seems like you need to read through a Git book or tutorial. – torek Sep 03 '17 at 19:50