Let's go fishing with git! 🎣
Background: I work on a team that supports a large, globally available video game. We are in the middle of implementing a set of features and there was a misunderstanding of where the data is to be saved in a database table for the user generated data service, but we have the exact code we are looking for in our git log.
I want to find the code that I wrote before so I don't have to think about how to do the task again because we spent that thought time already (Grug brain dev). my problem is I do not know the hash of what commit the code is in.
- I know git and grep and some basic Unix, so let's see what we can do
git loglets me see all of the logs, so that's usefulgrep -e 'pattern'helps search for text- git log
-pchanges the output to the content of the commit - the
|character takes output from command and pipes it into another command
git log -p | grep -e 'FirstPublishedDate' will get me closer but the output is rather noisy;
the
word
'FirstPublishedDate' is a common term in this development. Additionally, the output from
grep
is only
structured by if a line contains that phrase; it's a bit of a salad without the context of git. so let's
try
again:
git loghas-Gto do a search, and I can narrow it by the branch namegit log -G'FirstPublishedDate' -p 143495-back-populate-new-filter-valueswill narrow the search to the branch and do the filtering by pattern for me, and then-pwill show me the content- now let's use
grep -C 10to show the ten lines around it, and use a new keyword like@@to hone in on the hash.
Our answer is git log -G'FirstPublishedDate' -p 143495-back-populate-new-filter-values | grep -C 10 -e '@@'
with the result
Author: Michael Gardner
Date: Mon Feb 5 16:17:36 2024 -0500
removed unused function
Right at the top. A not so unused function after all 🎣