Advanced git archeology

I’m not that smart, but I know how to ask GPT

George Shuklin
OpsOps
2 min readFeb 9, 2024

--

The problem: I worked on refactoring; I did a few non-trivial renamings. I found big gaps in other places while doing it, which required me to abandon my current work and to spend a month (and a few hundreds of commits) on other changes. I finished those, and I wanted to return back to my original refactoring. I had already spent a few hours, thinking about names, writing neat comments, and I didn’t want to lose my results.

But: I forgot the branch name, and even if I find the name, the master moved so far away, that the diff between my work and the current master would be meaningless.

Here is my advanced archaeological session with git, assisted by ChatGPT.

Find a commit

I don’t remember anything, but one or two new names I invented. There were much more, but I remember only few. One of them was ‘bgp_si’. (Don’t ask me what it is and why, just trust me, it’s a good name).

Step 1: Find any commit around the work I’m interested in:

git grep bgp_si $(git rev-list --all)

It’s a super slow process. It took about a minute of 6 cores CPU in helicopter mode (fans 100%), but it showed me the commit.

Find the branch

There were few commits in that branch, and I need most of them

git branch --contains 2b0664b39395f5

2b0664b39395f5 is a commit I found in a previous step.

It gave me branch name: vars_redirects

Get the diff

Just plain git diff master..var_redirects won’t do any good. Changes are too big to be meaningful (>5000 lines).

Find the last common commit between my branch and master

git merge-base vars_redirects master

I get ba5d1a77e034d (yet another commit id).

Get the the the diff

git diff ba5d1a77e034d..vars_redirects

Bingo, I got my diff. It’s still 4700 lines long, but those are different lines compared to the diff with the master.

Moreover, I did a series of reasonably scoped commits, some very vast (but boring — move a few files around), and some are much smaller, but are the crème de la crème of my work.

git log ba5d1a77e034d..vars_redirects

Now I can enjoy not redoing the same creative work I did before and dedicate my time to less inspiring copy-paste from the diff, which I find easier to do. I don’t mind doing creative work (it’s satisfying), but I absolutely hate repeating it. I would better spend an hour mindlessly applying changes from the diff I found, than to re-think again about what the best way to name things is.

Huh. The archeology session is over, thanks for reading.

--

--

George Shuklin
OpsOps

I work at Servers.com, most of my stories are about Ansible, Ceph, Python, Openstack and Linux. My hobby is Rust.