Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git tracking block of code moved/removed in a file

I have an old commit md5hash on myfile.extension with SOME CHANGE in the commit body (not the commit title/metadata).

How can I generate a list of commits with SOME CHANGE modified (not just present) in the commits leading up to HEAD from md5hash without having to inspect each diff? (of which there are unfortunately many in the current case.)

I tried git rev-list --all | xargs git grep 'SOME CHANGE' but this seems to find all the commits with SOME CHANGE which is in the file.

git blame seems useless as the lines have changed and SOME CHANGE has moved.

like image 451
sjakubowski Avatar asked Nov 12 '15 21:11

sjakubowski


People also ask

How to see files changed in git?

you can also do: git diff --name-only HEAD@{3} HEAD@{0} for the exact commits you want to compare. As noted below, git diff --name-status doesn't seem to want to show added files. @sschuberth pointed out git show , which does seem to work properly for me: git show --pretty=format: --name-status .

How do I commit a staged change in git?

First, you edit your files in the working directory. When you're ready to save a copy of the current state of the project, you stage changes with git add . After you're happy with the staged snapshot, you commit it to the project history with git commit .

What file is needed in a project root to track the commits in git?

Make a file called git_steps. txt inside git_github . Record git commands you must use to start tracking your changes. Save this file and commit git_github directory to Git.


2 Answers

I think the answer you're looking for is git --no-pager log --pretty="%H" -G"SOME CHANGE" -- myfile.extension.

At first I thought of git log -S but it only covers add/remove. git log -G would probably be close to what you want. Here you can see the difference between -S and -G, and I've included the full commit history as well so you can see what's not covered. Read the commit messages for a description of what I did in the body.

# git --no-pager log --oneline -S"SOME CHANGE"
12e24ed Remove text
9427ffc Add the text
# git --no-pager log --oneline -G"SOME CHANGE"
12e24ed Remove text
6a33653 Change other text on same line
ac09bbb Append other text to same line
484b447 Move the text two lines down
9427ffc Add the text
# git --no-pager log --oneline
12e24ed Remove text
9c7f7d5 Change text on adjacent line
6a33653 Change other text on same line
ac09bbb Append other text to same line
484b447 Move the text two lines down
377936f Add other text on adjacent line
9427ffc Add the text
1929648 Initial commit

To get it with just the hashes:

# git --no-pager log --pretty="%H" -G"SOME CHANGE"
12e24ed749e499bc2d8920c5d8a3ca98a6422e3f
6a336532210ca85dea86968c34cef516345b8ab4
ac09bbb5c95bbea65e7c99c730653d27f90397f4
484b4478e4cb16c839dac558f3e958683b428a64
9427ffc7dd60a3cfb1d9880083e6262faea0eefb
like image 94
Michael Barker Avatar answered Sep 20 '22 00:09

Michael Barker


I think you are looking for:

git log -S<string>

Look for differences that change the number of occurrences of the specified string (i.e. addition/deletion) in a file. Intended for the scripter’s use.

It is useful when you’re looking for an exact block of code (like a struct), and want to know the history of that block since it first came into being: use the feature iteratively to feed the interesting block in the preimage back into -S, and keep going until you get the very first version of the block.

like image 37
arekolek Avatar answered Sep 20 '22 00:09

arekolek