3

I have two forks of the same git repository and I want to check whether they are absolutely identical and thus safe to delete one of them without losing any information.

While I know how to compare single branches, I want to ask whether there is a simple way to compare all branches, tags and so on.

cmbuckley
  • 40,217
  • 9
  • 77
  • 91
jotasi
  • 5,077
  • 2
  • 29
  • 51

3 Answers3

4

The quickest way to do this is to add a second remote and compare the output of git ls-remote for each:

diff -u <(git ls-remote --refs origin) <(git ls-remote --refs other)

The --refs option removes pseudorefs like HEAD, which may point at different branches on each remote.

You don't even need to add the remote to your local checkout; you can run something like git ls-remote https://github.com/username/repo.git.

If you see no output, the remotes are identical. If you see deletes in the diff, that corresponds to branches on origin but not on other; additions are on other but not on origin; and modifications are where that branch/tag exists on both but points at a different commit on each.

cmbuckley
  • 40,217
  • 9
  • 77
  • 91
  • 2
    Be aware that part of his question: "thus safe to delete one of them without losing any information" can't be answered by this simpler comparison. One remote might be ahead another, which makes that "another" safe to delete according to the OP, but this simpler comparison won't tell you this, it will only tell you that they're indeed different. – Lasse V. Karlsen Jul 27 '17 at 08:44
  • I'd go as far as to say if there is any diff, then it is not safe to delete either. The question only asks whether they are identical, rather than knowing whether one is ahead of the other; it's also perfectly believable that some branches on A are ahead of B, and some on B are ahead of A, so would need some deeper analysis with something like `git branch --contains `. – cmbuckley Jul 27 '17 at 08:56
1

Add repository B as a remote in repository A then, inside A, fetch from B:

$ git remote add B {local-path-or-url-of-B}
$ git fetch B

Now, all the branches and tags present in B also exist in A and it's easy to compare the local branches (find them using git branch -v) with remote branches (git branch -r -v).

Using a GUI Git client makes the comparison even easier.

--

At this point, almost everything that is in B is also in A (the stash is missing). You can use rebase, merge, cherry-pick etc. to append branches and commits imported from B into the branches present in A (or vice-versa).

You can remove the B repository, but don't remove the B remote of A until you are sure everything that is in B and you need in A can be reached using tags or branches of A.

axiac
  • 68,258
  • 9
  • 99
  • 134
0

It depends on what do you mean by "and so on" - it will not for example help you with hooks, .git/config and generally anything other than refs - but comparing the output of git show-ref in both repositories may be a simple way to detect whether they differ or not.

Michał Politowski
  • 4,288
  • 3
  • 30
  • 41