Week 3

Day 2 - "But I need more information"

Digging a little deeper

In the trenches...
"I know John, and next time I will make a note of it, but right now, I'd really like to know where this file got changed," Klaus pointed at the piece of paper containing a print out, "specifically when this function was introduced."

John smiled. His hands danced over the keyboard as he finished compiling an email. "And you've no idea when this was added at all?" he asked.

"No, sorry John, I don't." He pondered, "I guess I could write a script to untar all the versions we've created in the last week and search through them." He sighed, "Can't the wonderful Git help us out here?"

A head popped up over the cubicle wall. "You wanna find out when a function was introduced to a file?" It was Rob. "After John showed me the basics, I went and read up on it a little. Git has some really powerful searching within the log tool."

"Well come on then," blurted Klaus, "Don't keep me hanging on."

A chime of the popular 1966 hit sprang out in the office.

Klaus pulled a hand down over his face, "Oh don't you all start!"

Git can actually do some rather powerful searching to assist a developer in their daily tasks. It would have been useful if the particular item that was being searched for had been included in the log, but sometimes, things either get missed, or there are just too many changes introduced in one commit to list them all.

In these instances, the git log -S "<string>" command comes to our aid. This command will search through the commits in a repository and will return a list of commits which introduced or removed a specific string into the repository. First of all, let's run this against our test repository.
john@satsuki:~/coderepo$ git log -S "Change1"
commit 163f06147a449e724d0cfd484c3334709e8e1fce
Author: John Haskins <john.haskins@tamagoyakiinc.koala>
Date: Thu Mar 31 20:32:59 2011 +0100

Made a few changes to first and second files
john@satsuki:~/coderepo$

You can see that git log has shown us the commit that instantiated the change. As you can imagine, when using a large code base, this tool can be invaluable. It allows us to pinpoint a specific moment when a certain string of text entered the repository. When running this against a very large repository, this could take a long time, and so the ability to shrink the search scope down will result in a much faster result. To do this we can append a path to our previous command.
john@satsuki:~/coderepo$ git log -S "Change2" my_first_committed_file
john@satsuki:~/coderepo$ git log -S "Change2" my_second_committed_file
commit 9938a0c30940dccaeddce4bb2eb151fba3a21ae5
Author: John Haskins <john.haskins@tamagoyakiinc.koala>
Date: Thu Mar 31 20:34:23 2011 +0100

Finished adding initial files
john@satsuki:~/coderepo$

If you remember from our committing back in Week 2, we added the string Change2 to the second file but not the first. So the first time we run this command, it fails, as we are searching against my_first_committed_file. The second time we run it, we are searching against my_second_committed_file and this is where we see a result. Commit 9938a0c contains the change we are looking for.

Previous Day

Next Day

 
   
home | download | read now | source | feedback | legal stuff