Find when line was deleted

前端 未结 3 2083
礼貌的吻别
礼貌的吻别 2020-12-04 21:08

Earlier today I discovered that some code was missing from my Git repository. I knew some of the missing text, and the file that it was in, so I used git log -S\'missi

相关标签:
3条回答
  • 2020-12-04 21:16

    There is a great answer to this on Super User: Git: How do I find which commit deleted a line?

    git blame --reverse START.. file.ext
    

    This will show, for each line, the last commit where the line was present - say hash 0123456789. The next commit to follow will be the one which removed it. Use git log and search for hash 0123456789 and then its successor commit.

    0 讨论(0)
  • 2020-12-04 21:19

    Quick and dirty way #2 - use a for loop.

    for commit in $(git log --pretty='%H'); do
        git diff -U0 --ignore-space-change "$commit^" "$commit" | grep '^-.*missingtext' > /dev/null && echo "$commit"
    done
    

    This will include all merge changes because it explicitly specifies the base commit for the diff. I came up with this because git log -c -S... was giving me a bunch of false-positives. Also, when I specified a filepath in the initial git log command, it skipped the commit I was looking for.

    Since this may run for a while, you can specify -n on the git log command or put an && break at the end of the loop if you only need 1 result.

    0 讨论(0)
  • 2020-12-04 21:28

    git log doesn't show a diff for merge commits by default. The -c or --cc flags should do the trick:

    git log -c -S'missingtext' /path/to/file

    More discussion/explanation is here.

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