如何比较两个 Git 工作树或两个 Git 差异?

发布于 2025-01-10 09:55:17 字数 1270 浏览 0 评论 0原文

问题定义

我想同步两个都可以修改的文件夹。如果只有其中一个发生变化,处理起来很简单,但是当两者都包含一些更改时,我需要检查同一个文件在两个位置是否发生更改,我需要保留一个并在文件名后附加一个字符串其他。

我知道 Git 不是一个备份管理器,也不支持任何同步,但是,我认为 Git 非常擅长在两个版本的文件夹/文件之间创建差异。

我尝试

使用带有单个分支(main)和工作树(创建另一个分支,我们称之为aux)的本地存储库(无遥控器)。

当两个文件夹中发生更改时,我可以在 HEAD 之间进行更改(两个分支都指向同一个提交),但我不知道是否可以(1)比较两个差异以获取常见更改的文件;或 (2) 比较两个工作树。

在获取差异之前,我总是暂存所有更改,以防止任何后续更改妨碍,也因为我更喜欢 git diff HEAD --name-status --diff-filter=tu 的输出格式--find-copies-harder (--diff-filter=tu 只是为了以防万一,它应该不是必需的,至少在我的用例中不是)。

当然,我可以手动比较差异,但这似乎很费力:

  1. 获取差异;
  2. 获取所有更改的文件(对于重命名的文件,包括原始文件名和新文件名);
  3. 比较两个文件列表。

我希望有更好的方法。

TL;DR

有没有办法比较两个 Git 工作树(当前工作树和工作树的工作树中的实际暂存文件)?

如果不可能,我如何比较两个 Git diff(如果您愿意,可以是补丁)以查找常见的更改文件?

该解决方案需要使用 Git 命令或我可以在 Linux 终端(BASH、Perl 等)中使用的命令。

更新

更新 1

请注意,我不关心文件中发生了什么变化,而是关心什么文件发生了变化以及如何变化(添加、删除、修改、重命名、复制)

更新 2

对问题的部分 OT(但不是问题定义中描述的我的用例):是否可以在自动重命名冲突中的文件的同时合并两个工作树(或至少两个提交) ?例如,如果在两个树中修改了同一个文件(无论是否可以合并), git merge 将保留其中一个而不进行额外更改,而另一个将被重命名(例如${orig_filename}_dupe.${orig_file_ext}${orig_filename}_dupe_${number}.${orig_file_ext})?这将完全解决我的问题。

Problem definition

I want to synchronise two folders that can be both modified. If only one of them changes, it is quite simple to deal with, but when both of contains some changes, I need to check if the same file changed in both locations, I need to keep one and append a string to the filename of the other.

I know that Git is not a backup manager nor does it support any synchronisation in this sense, however, I think Git is a quite good at creation diffs between two versions of folders/files.

What I tried

I want to use a local repository (no remotes) with a single branch (main) and a worktree (which creates another branch, let’s call it aux).

When changes occur in both folders, I can the changes between HEAD (both branches point to the same commit), but I have no idea if I can either (1) compare the two diffs for comman changed files; or (2) compare the two work trees.

Prior to getting the diffs, I always stage all changes in order to prevent any subsequent changes to get in the way and also because I prefer the output format of git diff HEAD --name-status --diff-filter=tu --find-copies-harder (--diff-filter=tu is used just in case, it should not be necessary, at least not in my use case).

Of course, I can manually compare the diffs, but it seems to be much labourous:

  1. get the diffs;
  2. get all changed files (for renamed files both original and new filenames);
  3. compare the two lists of files.

I hope there is a better way.

TL;DR

Is there a way to either compare two Git working trees (actually staged files in current working tree and in working tree of a worktree)?

If it is not possible, how can I compare two Git diffs (patches if you wish) for common changed files?

The solution needs to use either Git commands or something I can use in Linux terminal (BASH, Perl, …).

Updates

Update 1

Note that I don’t care about what changed in files, but about what files changed and how (added, removed, modified, renamed, copied).

Update 2

Partially OT to the question (but not to the my use case describe in the Problem definition): is it possible to merge two working trees (or at least two commits) while automatically renaming files in conflicts? For example, if in both trees the same file is modified (no matter if it can be merged or not), git merge would keep one of them without additional changes and the other would be renamed (e.g. to ${orig_filename}_dupe.${orig_file_ext} or ${orig_filename}_dupe_${number}.${orig_file_ext})? That would solve my problem altogether.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文