Five Ways to Be More Productive with Git

Last updated on by

Five Ways to Be More Productive with Git image

In this post, I've gathered some of my favorite tools to make working with Git more productive. I'll cover different types of tools, ranging from git aliases, git tools, git workflows, and more.

Useful Git Aliases

I use git from the command line, so aliases are essential to my workflow to shorten the number of characters I need to type and automate multi-step workflows into simple-to-use aliases. I can't possibly cover every awesome alias—I encourage you not to go overboard with the number of aliases you have—so share your favorites with us on social media.

"git nah"

Probably THE most popular alias out there is the git nah alias, and lucky for you, we shared The Ultimate "git nah" Alias from developer Liam Hammett.

If you're working on a feature and you start modifying/creating files, moving things around, and just want to start over, you can run git nah, and it's like it never happened (we won't tell).

The ultimate nah alias goes above and beyond the simple git reset --hard, and does a few more things:

  • Do a --hard reset
  • Run clean -df to clean up directories and files
  • Run rebase --abort if you have a rebase in progress

Here's the alias you can add to your ~/.gitconfig file:

# Git alias ⬇️
[alias]
nah = "!f(){ git reset --hard; git clean -df; if [ -d ".git/rebase-apply" ] || [ -d ".git/rebase-merge" ]; then git rebase --abort; fi; }; f"

git forget

One of my absolute favorite aliases for cleaning up local branches after merging them is my git forget alias. It fetches the remote, lists branches, filters branches with : gone], and deletes the local version.

My workflow is roughly:

  1. Create a feature branch from main
  2. Work on feature / pull request
  3. Merge to main
  4. Delete remote branch and delete local branch

I can configure GitHub to delete a branch automatically after it's merged; however, cleaning up branches locally is a manual process. I don't necessarily run this command immediately after a merge, but it's an excellent way to clean up those branches that are merged.

If you want to try it out, add the following to your ~/.gitconfig file:

[alias]
forget="!git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D"
forgetlist="!git fetch -p && git branch -vv | awk '/: gone]/{print $1}'"

The forget alias is destructive, so I have a read-only alias as well if I want to see which branches will be deleted first:

git checkout main
git forgetlist

Be aware that this alias won't delete branches that still have a remote counterpart. The magic is in the -vv flag of the git branch command, which is described as follows:

-v, -vv, --verbose
When in list mode, show sha1 and commit subject line for each head, along with
relationship to upstream branch (if any). If given twice, print the path of the linked
worktree (if any) and the name of the upstream branch, as well (see also git remote show
<remote>). Note that the current worktree's HEAD will not have its path printed (it will
always be your current directory).

The -vv flag prints the linked worktree's path and the upstream branch's name. Here's what it might look like if you have a local branch that no longer has a

git branch -vv
* demo-branch b71981eb [origin/demo-branch: gone] chore: bump version
development bd951668 [origin/development] example message
...

Note the gone ] part, which is what the alias uses to batch branches that no longer have an associated remote branch.

git uncommit

Similar to git nah, the git uncommit alias is helpful if you haven't pushed code to a remote repository and you want to remove the commit:

[alias]
uncommit = reset --soft HEAD~1

This is just a shortcut for git reset --soft HEAD~1, which essentially rewinds the branch by one commit and keeps the changes from that commit in the staging area.

Configure a Git Commit Template

You can configure a global commit message template that is used when you run git commit without the -m|--message flag. You can configure this template as part of a repository or configure it globally.

# Configure a commit.template globally
git config --global commit.template ~/.gitmessage
 
# Configure a commit.template in a project
git config commit.template .gitmessage

Here's my global template, which I like to use to remind myself of not only the how of a commit, but also the why and the when:

[one line-summary of changes]
 
Because:
- [relevant context]
- [why you decided to change things]
- [reason you're doing it now]
 
This commit:
- [does X]
- [does Y]

Using a Password Manager for SSH Keys and SSH Key Signing

I've recently switched to managing my SSH keys for GitHub and other servers through 1Password, which you can use as your SSH agent. Using 1Password as my agent also means that I can authorize SSH operations via biometric scanning, and I don't even have to have an SSH key pair in my ~/.ssh folder. When using 1Password as an SSH agent, my private key never leaves 1Password!

If I ever need to create a new key, I can generate one within 1Password and copy the public key to my GitHub.com settings or whatever server I need access.

With 1Password, I have a separate SSH key for key signing on GitHub. I recommend reading Telling Git about your signing key and Sign your Git commits with 1Password for more information. Other tools offer SSH agent features, so I'd recommend you do some research if you use something other than 1Password.

GitHub CLI Tool

The gh CLI tool isn't anything new or unique to my setup, but I enjoy using the command line to work with Git, and when I am ready to create a pull request, I enjoy using the GitHub CLI to do so without leaving the terminal using gh pr create:

Using gh pr create to submit a pull request on GitHub
Using `gh pr create` to submit a pull request

I usually select Continue in browser, which opens the GitHub pull request UI with all the pull request information filled out. This lets me check the file diff and submit the pull request.

The gh CLI is also useful for many other things related to managing your code on GitHub; here are a few of my favorites that I've incorporated into my workflow:

# use the --watch flags to watch them until they finish 🔥
gh pr checks
 
# Get the status of a pull request for your current branch (if any)
gh pr status
 
# Really useful when reviewing pull requests
gh pr checkout 123
 
# List open issues
gh issue list

Configure a Git Merge/Difftool

If you use git from the command line, you might fix merge conflicts by hand or you're smart enough to use the built-in git mergetool tool (I am not). Sometimes, I jump into a GUI tool (i.e., PhpStorm) directly to merge conflicts when I run into a conflict or edit. Another thing you can do is configure a different GUI merge tool to work through conflicts and view diffs in a GUI:

[diff]
tool = opendiff
[mergetool]
keepBackup = false
[merge]
tool = opendiff

Note: you don't need keepBackup = false if you want to have a backup around during a merge. You'll have to clean these up or ignore them though if you don't disable backups.

When I run git mergetool during a conflict, it will use opendiff, which opens the FileMerge program to give me a visual diff tool. I can also run git difftool to visualize the diff in FileMerge.app.

If you made it this far, I hope you've found a few things to improve your Git workflow! Let us know your favorite Git workflows, tools, tips, and aliases on your favorite social network!

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Learn to master Query Scopes in Laravel image

Learn to master Query Scopes in Laravel

Read article
How to Redirect Uppercase URLs to Lowercase with Laravel Middleware image

How to Redirect Uppercase URLs to Lowercase with Laravel Middleware

Read article
Generics Added to Eloquent Builder in Laravel 11.15 image

Generics Added to Eloquent Builder in Laravel 11.15

Read article
Manage Roles and Permissions in Filament with Hex Lite image

Manage Roles and Permissions in Filament with Hex Lite

Read article
Laravel Rest Api now supports Laravel Scout image

Laravel Rest Api now supports Laravel Scout

Read article
Build Your SaaS App in Days with SaaSykit image

Build Your SaaS App in Days with SaaSykit

Read article