Git with Github is probably the most widely used version control systems across enterprises, all throughtout the world. As a developer, I have had my far share of working with Git throughout my career. In this post, I will be listing some practices and configurations that have helped me improve my productivity with Git.

Branch Protection Rules

As a person who has prefers Trunk Based Development, my concern when setting up a multi-user code repository will usually be how I could prevent accidental force pushes, deletions, how other teams should make changes to my code and how it should be accepted, etc.

To address this, Github has the provision to setup branch protection rules. This setting allows you to specify who can force push, disable branch deletion, mandate commit signing, setup status checks for merges etc, based on a branch name or a branch name pattern. We can set this up by going to the Settings tab on the Github repository.

Check out the how to setup branch protection and the various rules you can add to your repository on Github.

Auto Stash

When working with Git, I used to have some difficulty getting my branch up to date, if my working directory was dirty with some changes. I would end up stashing those changes, pulling from remote to bring my working directory up-to-date, and then popping the stashed changes again.

I longed for this to be automated, and that’s when I discovered the autostash option with git.

To enable autostashing on pulls, we can run

git config rebase.autoStash true # If your default pull behavior is to rebase
git config merge.autoStash true # If your default pull behavior is to merge

All though there could be some conflicts between the pulled and stashed changes, we should easily be able to resolve them.

Command Line Aliases

I usually use the command line to work with Git in my projects. Since I usually use a Macbook for development with a ZSH kernel, I found it easy to setup oh-my-zsh with the git plugin. The plugin powers my command line with a comprehensive list of aliases for git commands.

Now instead of typing in

git checkout -b new-branch
git push --set-upstream origin new-branch

I can just do

gco -b new-branch
gpsup

Git Hooks

A usual practice with git is to use git hooks to perform some custom operations like linting, running tests before push, detecting and installing dependencies etc. This can help us set up some code quality assurance and provides confidence on the standard of code we push.

Since I work mostly on JavaScript based projects, I found Husky to be very useful and easy to set up git hooks on these projects.

These were some practices that improved my development flow with Git