Diffing between two entire directories/projects in hg or git?
Asked Answered
P

8

87

I inherited a project originally stored in CVS with all the revisions. I made quite a few edits, and I'm trying to compare all the changes I made in the original directory, in regards to new files added versus the old ones.

Is there some sort of utility for hg/git where I can do a tree diff, or something of that nature? So that say, there's a mark between newly added files, deleted files, am I asking for too much?

Partheniaparthenocarpy answered 24/11, 2009 at 18:3 Comment(1)
https://mcmap.net/q/55761/-differences-for-a-certain-folder-between-git-branches-duplicate git diff master..yourbranch path/to/folderCreamy
S
5

git diff does exactly that. but it only works for git projects.

hg diff, svn diff pretty every version control system can diff directory trees

Sigmatism answered 24/11, 2009 at 18:10 Comment(5)
Can you expand on how I can use git diff? I created two separate git projects, how can I compare them now? I'm still relatively new to VCSing.Partheniaparthenocarpy
you only need one project. commit all your files, make your changes, commit again. you can then do git diff HEAD^..HEAD, which diffs your latest version against the one before itSigmatism
Ok - I guess I got what I wanted because I'm seeing a ton of results, can you recommend a nice GUI?Partheniaparthenocarpy
try gitk and git gui, gitk visualizes project history, git gui allows you to prepare commits and suchSigmatism
Actually git diff can compare arbitrary files/directories; you might need to use its --no-index optionTrephine
T
141

To simply create a diff patch in git's diff format from two arbitrary files or directories, without any fancy repository stuff or version control:

git diff --no-index some/path other/path > some_filename

Jakub Narębski's comment on knittl's answer hinted at the answer... For simplicity's sake, that's the full command.

The > part creates a file and redirects the output to it. If you don't want a file and just want the output printed in your console so you can copy it, just remove the > some_filename part.


For convenient copying and pasting, if you've already cded to a directory containing the original directory/file named a and the modified directory b, it'll be:

git diff --no-index a b > patch
Tabanid answered 30/10, 2011 at 15:54 Comment(3)
To use git diff options, add the options before the paths. E.g. git diff --name-status --no-index some/path other/path works with the --name-status option.Deerdre
use --binary for binary files, otherwise the patch will just say "Binary files differ"Leakey
From git-diff manpage: "You can omit the --no-index option when running the command in a working tree controlled by Git and at least one of the paths points outside the working tree, or when running the command outside a working tree controlled by Git."Leakey
C
25

From git diff manpage:

git diff [--options] [--] [<path>...]

[...]
If exactly two paths are given, and at least one is untracked, compare the two files / directories. This behavior can be forced by --no-index.


If you want to compare two versions (e.g. two tagged releases, or two branches) in two different repositories, you can use trick described in GitTips page on Git Wiki, under "How to compare two local repositories".

Assuming that you are inside one repository, and second repository is in /path/to/repo, so its GIT_DIR is /path/to/repo/.git if it is non-bare repository, you can something like the following:

$ GIT_ALTERNATE_OBJECT_DIRECTORIES=/path/to/repo/.git/objects \
   git diff $(git --git-dir=/path/to/repo/.git rev-parse --verify A) B

where A and B are revisions you want to compare. Of course you can also specify path limiter in above expression.

Explanation: GIT_ALTERNATE_OBJECT_REPOSITORIES variable can be used to make git commands concatenate object database of the two repositories. git --git-dir=... rev-parse ... is used to turn name (extended SHA-1 expression) in repository given as parameter to git-dir option into unique SHA-1 identifier. The $( ... ) construct puts result of calling given command in command line. git diff is used to compare two revisions (where one is from alternate object repository).

Alternate solution would be to simply import other repository into given repository, using git remote add (and git fetch). Then you would have everything locally, and would be able to do comparision inside single repository.

Couchman answered 24/11, 2009 at 19:47 Comment(0)
D
18

Is there some sort of utility for hg/git where I can do a tree diff... [s]o that say, there's a mark between newly added files, deleted files... [emphasis added]

Yes. We can git diff the current directory against another directory and...

...mark the added, deleted, and modified files:

git diff --name-status --no-index ./ path/to/other/dir

...show only added files:

git diff --diff-filter=A --name-status --no-index ./ path/to/other/dir

... show only deleted files:

git diff --diff-filter=D --name-status --no-index ./ path/to/other/dir

...show only modified files:

git diff --diff-filter=M --name-status --no-index ./ path/to/other/dir

See also: https://git-scm.com/docs/git-diff

Deerdre answered 17/1, 2017 at 20:0 Comment(0)
H
11

It's not git-specific, but as a general diff utility for Windows, I really like WinMerge.

Hirai answered 24/11, 2009 at 18:13 Comment(0)
C
8

I don't really understand what you want, but isn't diff -ur enough for you? It will work even on directories without any kind of version control.

Craze answered 24/11, 2009 at 18:41 Comment(2)
Not sure why -u is part of this answer. -r goes recursively through directories. -q is very useful if you don't want to see all the diffs, but just which files changed.Spillman
Yup, -u is for unified diffs. Nothing to do with recursion per se.Craze
S
5

git diff does exactly that. but it only works for git projects.

hg diff, svn diff pretty every version control system can diff directory trees

Sigmatism answered 24/11, 2009 at 18:10 Comment(5)
Can you expand on how I can use git diff? I created two separate git projects, how can I compare them now? I'm still relatively new to VCSing.Partheniaparthenocarpy
you only need one project. commit all your files, make your changes, commit again. you can then do git diff HEAD^..HEAD, which diffs your latest version against the one before itSigmatism
Ok - I guess I got what I wanted because I'm seeing a ton of results, can you recommend a nice GUI?Partheniaparthenocarpy
try gitk and git gui, gitk visualizes project history, git gui allows you to prepare commits and suchSigmatism
Actually git diff can compare arbitrary files/directories; you might need to use its --no-index optionTrephine
S
1

There Are Following Another Ways For Diffing between two entire directories/projects.

  1. In Git There is Syntax:

Syntax: git-diff [] [--] […​] This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you could tell Git to further add to the index but you still haven’t.

Here is the. URL: https://git-scm.com/docs/git-diff

git diff --no-index directory 1 project /path directory 2 project/path >> File name

  1. Using Linux Command diff --brief --recursive dir1path/ dir2Path/

  2. If you are using windows there is an application WinMerge.

Sheasheaf answered 12/2, 2020 at 6:17 Comment(0)
L
0

Creating the patch from dir1 to dir2 (--binary only needed for binaries):

git diff --no-prefix --no-index --binary dir1 dir2 > dir.diff

Applying the patch to contents of working directory, which has the same contents as dir1:

cd dir1_copy
git apply ../dir.diff

git apply has default -p1 which strips the leading directories in diff commands.

Leakey answered 12/7, 2020 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.