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

筅森魡賤 提交于 2019-11-26 19:14:14

问题


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?


回答1:


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




回答2:


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



回答3:


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.




回答4:


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




回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/1791854/diffing-between-two-entire-directories-projects-in-hg-or-git

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