I'm trying to figure out whether a merge conflict was responsible for a bug, but I'm having difficulty because I'm not clear on how conflict resolution affects git blame
.
Let's say I have a file in master:
a();
b();
c();
I modify it in master:
a();
d();
c();
but so does a co-worker, and they modify it differently, in a separate branch which they then merge in to master:
a();
e();
c();
Can resolving that conflict affect the blame? In other words, if my co-worker resolves the conflict by going with my version:
a();
d();
c();
and I git blame
the d();
line, who will be blamed: me or my co-worker?
Similarly, let's say git got confused and thought both the first and second lines were conflicted:
<<<<
a();
d();
====
a();
e();
>>>>
If my co-worker resolves the conflict with their version:
a();
e();
c();
and I git blame
the a();
line, will I (the line's original author) get the blame, or will my co-worker (who "touched" it last, even though they didn't change it) get the blame?
Git can fail during the merge This occurs because you have committed changes that are in conflict with someone else's committed changes. Git will do its best to merge the files and will leave things for you to resolve manually in the files it lists.
By default, when Git sees a conflict between two branches being merged, it will add merge conflict markers into your code and mark the file as conflicted and let you resolve it.
If you run git blame
on a merged file you are going to see the original author of each line, regardless of who did the merge commit. This means that if your co-worker decided to resolve the conflict using your version of the line, your name will be shown next to it.
In Git each commit holds two key pieces of information:
Given these two facts Git can reconstruct the history of changes occurred in a file by walking backwards from a given commit, generating at each step a diff between the current version of the file and its previous one.
This is exactly what git blame
does. When you do git blame
on a file, Git will reconstruct the history of the file on a line by line basis, showing you the author of the commit that introduced (i.e. added) each line.
A merge commit holds a reference to two parents:
The snapshot contains all the changes from each side combined.
If you do a git blame
on a merge commit, Git will walk two lines of history, one for each of the parent commits. Once Git reaches the commit whose snapshot added a certain line, it will show that commit's author next to the line.
If a new line – that is a line that doesn't belong to either side of the merge – was added as part of a conflict resolution, that line would belong to the snapshot referenced by the merge commit itself. In that case git blame
will report the author of the merge commit for that line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With