0

Can I compare branches by content rather than commits using Git? I almost see that I can't. Why?

Explanation: I have branch A which I merged with B , but then later in history I reverted that merge in A.(That revert wasn't actually a revert, but merge from pull , because I tried to push before I pull and branch got diverged which led me into merge that reverted my previous merge). And now B and A show no difference because they are compared by commits rather than content.

C0---C1---C2---C3---C4---C5    -> branch B
               /
        C1--C2   -> branch A

Steps I am doing:

  • C3 - is the merge commit from branch A to branch B
  • C2 - is where my head is locally (behind)
  • Right now I want to push my locally made commit(C4) to remote branch B which leaves my local branch B diverged and I need to pull before push. Which produces merge between C3 and C4.
  • However I am not careful and my pull's merge reverts my previous merge from branch A to branch B(C3) and all changes from C3 are gone.

What I want to do right now is to merge branch A to branch B again like I did(C3) , but when I compare them it shows no difference , because C1 and C2 commits are actually in branch B and there is no commit difference , but the content is different because I reverted them by accident in my pull's merge(C5). And that led me to the question why and is there a way of git comparing branches by content rather than commit hash?

kuskmen
  • 3,648
  • 4
  • 27
  • 54
  • 2
    They *are* compared by content. That's why you see no differences. Git is telling you that the content is the same. – Greg Burghardt Jun 14 '16 at 12:43
  • 1
    @Greg No, they are compared by commits. Thats why I see no differences, at least thats how I understand it. Also if you explain the down vote it would be awesome. – kuskmen Jun 14 '16 at 12:44
  • How do you perform comparison? what utilities or shell commands are you using to do it? that's quite important. Also, how did you "revert" that merge? did you do a reset, or an actual revert operation? if you look at the current history as graph, is the merge still visible in A/B history? – quetzalcoatl Jun 14 '16 at 12:45
  • @quetzalcoatl I am actually using stash to compare them. But I guess they are doing it with something like `git diff A B` I will edit my question. – kuskmen Jun 14 '16 at 12:48
  • Huh. I don't understand at all. Do you mean `git stash` console command? how do you use it to compare branches? I can't see a way to do it. Or did you mean Attlassian Stash web portal for Git repos? – quetzalcoatl Jun 14 '16 at 12:59
  • @quetzalcoatl Attlassian Stash web portal - sorry. – kuskmen Jun 14 '16 at 13:00
  • I allowed myself to add a tag, I hope you don't mind. It may help others later. By the way - I noticed your edit and I almost understand your explanation of push/merge/pull chain.. but, almost. Could you produce something like a http://stackoverflow.com/questions/1838873/visualizing-branch-topology-in-git to show it? It may be necessary to add 2 or 3 graphs, for example, to show how it was, what happened, and how it is now. Sorry if asking for much work, but I know that such things really help people respond quicker/better – quetzalcoatl Jun 14 '16 at 13:05
  • @kuskmen: one quick hint - if you browse the history of branch A and of branch B and (..) you don't see your commits you want to restore, `git reflog` may help you find them. But that's far fetched guess. Please try to write down the branch/commits charts first and tell us at least what you saw back then, what you see now. – quetzalcoatl Jun 14 '16 at 13:13
  • Ok, so if I understand correctly, you want to revert all these merges (which I don't quite understand without drawing a tree). If yes, I'd suggest using `git reflog` to show your repo changes (including merges, reverts, etc). Then find the desired point/reference and create a new branch from it (`git checkout -b new-branch-name HEAD@{XX}`). Or you may want to rebase master based on this ref. Just be sure to backup your repo before doing anything! ;-) – jweyrich Jun 14 '16 at 13:13
  • hm.. that reminds me that I once found on SO a command `gitk --all $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )` (this requires GITK, but you often have it already; anyways if you dont have it, then the inner part is the most important). I found it very handy when looking for lost commit hashes. – quetzalcoatl Jun 14 '16 at 13:17
  • 1
    I feel like there is a question here, but I don't think we have enough information. The title of your question is actually how Git works. Something else is going on. We need to see the series of Git commands you did to get into the current state, and even post the results of a `git status` command if possible. – Greg Burghardt Jun 14 '16 at 13:22
  • I edited my question , I know it is a bit confusing but this is the all information I can provide right now. If any further questions appear ask I will answer! – kuskmen Jun 14 '16 at 14:15
  • I too have the same issue and am interested in the solution. After reverting a merge request with using gitlab, `git diff develop..mybranch` shows zero diff, while I know the branches have different content. – Meglio Jul 18 '17 at 09:00
  • Yes @Meglio , this is the case I am worried also because it scares our tech leads of reverting merges in master branch and I want to make things better sadly no solution still – kuskmen Jul 18 '17 at 11:52

0 Answers0