How to search a git repository history to find a merge error?

前端 未结 5 1242
半阙折子戏
半阙折子戏 2021-02-09 00:17

At some point in our past branches of development in git were merged. However, the wrong merge decision was made and therefore some code didn\'t make it into master branch tha

相关标签:
5条回答
  • 2021-02-09 00:36

    Without more details I can only hint at possible solutions. If you know the file or line affected, you can try either git-blame (git blame *file*, or git blame *revision* *file*), or you can try so called 'pickaxe search' with git-log, i.e. git log -S'*line* trying to find revision which introduced given line, or deleted given line. You can find and examine all merges, for example via git log -p -m --grep=Merge, and examine how they relate to their parents (-m show diffs to all parents; alternatively -c shows combined diff but doesn't show trivial merge changes, i.e. if one side was taken).

    0 讨论(0)
  • 2021-02-09 00:48

    If you know a line in a file that was modified by the git branch merge, you can do 'git blame file.txt' and determine the commit hash number and commit author of the line in the file. Then you can go through the git log and pull up the exact commit associated with the bad branch merge.

    EDIT: In response to the author's comments, if you're looking for the disappearance of a certain line then 'git diff' combined with grep and binary search could be what you want. let's say you have commit numbers 0,1,2,3,4,5,6. You know that the line existed in revision 0, but disappeared in revision 6. Use 'git diff' plus grep to search for the disappearance.

    git diff 0 6 | grep '- line I care about'
    

    The first iteration, you'll see the line you care about disappearing. Then you cut the revision number in half and try again

    git diff 0 3 | grep '- line I care about'
    

    If the grep still shows the line disappearing ( with the '-' sign), then you know that the line disappeared in revision 0 to 3. If the grep doesn't show the line disappearing, then the line disappeared in revisions 4-6.

    Keep cutting the revisions in half until you find the culprit.

    0 讨论(0)
  • 2021-02-09 00:48

    Can git-bisect help in your case?

    0 讨论(0)
  • 2021-02-09 00:48

    Git log has powerful searching options. Since there's the indication you might know a chunk of code that disappeared, you can search for that string of code

    git log <HERE>..<THERE> -S"line I care about" --diff-filter=M

    Will search from HERE to THERE for the string after -S and only where the line was modified (added or removed)

    You can achieve even more precision in your search if you use -G instead of -S. -G provides regular expression searching instead of string literal searching with -S.

    0 讨论(0)
  • 2021-02-09 00:53

    For others looking for a simpler solution, fire up gitk (or from Git GUI go to Repository > Visualise X History) and use its find tool.

    0 讨论(0)
提交回复
热议问题