Avoiding context-switching with Git automation

How did I use a pre-push Git hook to keep me focused and avoid context switches.

Daniel Shterenberg
Wix Engineering
3 min readSep 26, 2021

--

Photo by Clément Hélardot on Unsplash

Are you familiar with the overwhelming feeling of starting a new role and having hundreds of open Chrome tabs with different information about your new domain, new coding language, and other new interesting resources you want to go over? This was my exact situation back when I started my new software engineering role @ Wix.

Every time I had to open a web page, I got into a tab-vortex, diving into many different subjects and completely losing my focus and context.

This problem is just getting worse while working with a version-control web client. For every PR (pull request) created on GitHub, I had to go to the browser and this cycle started all over.

I felt like this process is slowing me down, and therefore, like with many things in my life, I decided to try to break it. After some thinking, the idea of a pre-push hook came to mind.

Git hook?

If you are not familiar with the term, a Git hook here is an explanation from the Atlassian tutorial:

Git hooks are scripts that run automatically every time a particular event occurs in a Git repository. They let you customize Git’s internal behavior and trigger customizable actions at key points in the development life cycle.

Every Git repository has a .git/hooks directory containing the hooks. This directory is automatically being populated with example scripts (.sample) when the repository is initialized. In order to use these scripts, we need to delete the .sample extension.

Pre-Push hook?

The pre-push hook runs during git push, after the remote refs have been updated but before any objects have been transferred. It receives the name and location of the remote as parameters and a list of to-be-updated refs through stdin.

So what did I actually do?

I added to my repositories a pre-push hook which on every push to master, outputs in my terminal the link to the created PR. Clicking on the link, takes me to the PR and by that, I’m avoiding the manual browsing to Github and the PR searching, and obviously, this prevents me from wandering around the corners of the internet.

So “how does it look?” you ask.. this is it:

As you can see, it is not long, but it does the job. Just copy-paste these lines into a new pre-push file (notice the lack of an extension) and remember to add permissions to run this file (You can do so by running chmod +x pre-push)

What if I already have a pre-push hook in place and I want to add this one as well?

Because there can only be one “main” pre-push hook, so there are few ways to handle this situation:

  1. You can probably do this manually by examining the two hooks and merging my code into your existing code.
  2. My preferred way of handling this will be by creating a pre-push.d directory inside your .git/hooksfolder and moving the pre-push scripts to this new folder. It will look something like this:

Now, copy the following script into your pre-push script.

This script will run all the scripts under the pre-push.d directory.

Now that you have everything in place, whenever you will push to master, you will get a link to the PR in your terminal. Follow the link and avoid distractions :)

Happy focused coding!

--

--

Daniel Shterenberg
Wix Engineering

Passionate about basketball, software engineering and the mesh of them. I can also talk about ML, Crypto and Math. Msc in computer science. Never divided by 0.