Five Ways to Be More Productive with Git
Last updated on by Paul Redmond
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:
- Create a feature branch from
main
- Work on feature / pull request
- Merge to
main
- 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 maingit 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 globallygit config --global commit.template ~/.gitmessage # Configure a commit.template in a projectgit 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
:
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 requestsgh pr checkout 123 # List open issuesgh 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!