0

How to get diff, including all file changes, for a list of commit ids?

I have a list of commits from 2 branches, I would like to find the diff and confirm if all the commits from branch 1 are already available in branch 2.

The list is around 100+ commits, and i would like to get a single diff file for it.

  • What are you trying to achieve, specifically? Do you need the diffs for anything more than for comparing the two branches? – werkritter Jul 15 '15 at 18:45
  • Yes, due to some error two branches are corrupt, i have a list of 100 plus commits which are to be done on top of a known stable branch. –  Jul 16 '15 at 05:39

2 Answers2

0

For each commit you can use

git branch --contains <commit>

See git-branch

Than it is just a matter of wrapping it in a script and run it for each commit in your list.

Igal S.
  • 13,146
  • 5
  • 30
  • 48
0

Basic method

The most basic answer to your question would be to use git patch-id. See the description from git-patch-id(1):

A "patch ID" is nothing but a SHA-1 of the diff associated with a patch, with whitespace and line numbers ignored. As such, it’s "reasonably stable", but at the same time also reasonably unique, i.e., two patches that have the same "patch ID" are almost guaranteed to be the same thing.

IOW, you can use this thing to look for likely duplicate commits.

However, git patch-id has somewhat clunky interface — it demands a diff file on standard input. So in your case, for a commit with id COMMIT-ID, you could use:

git diff COMMIT-ID~ COMMIT-ID | git patch

Be wary that output checksum of git patch is different for some differences in input diff (for example, if you add the -U option, checksum changes).

There is a better way!

It turns out that there is a tool which wraps git patch-id. To get all commits from branch ONE, diffs of which aren't available on branch TWO just use git log with some extra options:

git log --cherry-pick --right-only --oneline --no-abbrev-commit TWO...ONE

The "business" options are --cherry-pick and --right-only. Please see git-log(1) — they are described very clearly there.

Getting diff?

As to getting a single diff from these commits, I'm afraid that's not possible if they won't happen to be consecutive. Remember that diffs are based on the previous state (commit) in the repository. Your best bet would be to cherry-pick the commits to a stable branch and brace yourself for some conflict resolving.

Community
  • 1
  • 1
werkritter
  • 1,479
  • 10
  • 12
  • Note: `git patch-id` should now *properly* reports all differences (attributes or binary) with [Git 2.39 (Q2 2022)](https://stackoverflow.com/a/63674369/6309). – VonC Oct 31 '22 at 16:14