Supercharge Your Git Workflow

Published September 17th, 2020
9 minute read

All too often, we settle into a git workflow that gets us by, but we stop there. When was the last time that you improved your workflow? This article will show you how I've supercharged my git workflow and explain how you can do the same. ⚡

I'll focus on Github since that's what I use, but many of these tips apply to any service provider.

There are a few main components to the usual git workflow

Git With It

What kind of article would this be if there wasn't at least one crappy pun?

Using git on your local device is all about managing repositories. Cloning them, branching, committing changes, stashing stuff, syncing changes back and forth from a remote, all sorts of git fun. There are a ton of ways to do this, but the command line is king. Sure, you can learn one of the many GUI applications, but if you master the command line version, you basically know them all!

Improving your workflow isn't about memorizing every command. Instead, strive to become a master of the ones you continuously use, day after day. For me, efficiency in this area comes down to one thing: aliases. Although I'm using zsh nowadays (I was a longtime bash holdout), these aliases should work for bash and be easily adapted to other shells. Try these out in your workflow and tell me how you like them!

Git Add & Commit
Stage all modified files and commit them — all at once! You can even pass in extra flags to commit. For example, pass in the -m flag to use it as a one-liner: gac -m "your neat changes"

alias gac='git add . && git commit'

Git Status
Simple enough, just shorter. Shows the status of the git repository in the current working directory.

alias gs='git status'

Git Diff
Show uncommitted, unstaged changes. Super useful to double-check changes or review. You can also type gd --cached to show staged (but not yet committed) changes. You might even go crazy and make an alias especially for that, like gdc.

alias gd='git diff'

Git Log
Show a summary of the last dozen commits — useful when you need to revert or see recent history.

alias gl='echo "Showing last 12 commits..." && git log --oneline --max-count="12"'

Git Branch
Another short alias that I use all the time. Two characters are better to type than ten.

alias gb='git branch'

Git Checkout
Switching between branches is something you do all the time. Checkout is also a long word to type.

alias gco='git checkout'

Publish Changes to Remote
My favorite alias. I rarely (if ever) use git push anymore. If there's one thing you try from this article, this should be it. This command pushes your current branch to the origin repository (Github). It is also smart enough to pass the -u flag to set the upstream repository when pushing a new repo.

Type pub to publish your changes to Github and forget about specifying branches or flags.

function pub {
    [[ $(git config branch.$(git symbolic-ref --short HEAD).merge) = '' ]] 
    && git push -u origin $(git symbolic-ref --short HEAD) || git push
}

Unstage Changes
Sometimes you want to unstage all your changes but keep your work. Use unstage for this.

alias unstage='git reset HEAD'

Delete Merged Branches
I always joke about this being my "Dave Matthews Band" command. Great for cleaning up local branches that are already merged on the origin repo (Github) side.

alias dmb="gco master && gb --merged | grep -v '\*' | xargs -n 1 git branch -d"

YAGNI - You Ain't Gonna Need It
No, not the extreme programming principle. It gets rid of anything you changed since the last commit made. As in, "Nah. you ain't gonna need it." Warning, this command can be destructive.

alias nah='git reset --hard && git clean -df'

Work In Progress
Writing perfect commit messages is overrated. After all, you can always rebase later.

alias wip='git add . && git commit -m "wip"'

Try out these aliases and tweak them to fit your workflow. Those precious seconds you'll (over and over again) quickly add up once they become second nature!

Git and Your Editor

Okay, maybe "my editor" because we're only going to cover VS Code.

It seems like most modern editors boast pretty serious git integrations. Implementing git in your workflow is easier than ever! Remember though, if you master it on the command line, you'll understand the features in the editor much better. If you're not using VS Code, you should go ahead and skip this section.

The git integration in VS Code is pretty good out of the box. Combined with the command line aliases above, there are three features that I use all the time. There's no wrong way to go about this stuff. This workflow is what works for me, so your preferences may vary.

The default key to bring up the source control sidebar is Ctrl+Shift+G. This hotkey sort of sucks, so I suggest you remap it to a less contorting variant, such as Cmd+K+G.

Diffing Changes
Sometimes you have a many changes, and looking at the files with ~~git diff~~ gd isn't ideal. Open up the sidebar and click on the file you want to view.

Git VSCode Diff

See that little lightbulb icon? Click that, and you can easily revert the change or copy the deleted line. I can't say I've ever used that feature, but it's there. You also may prefer a side-by-side diff as you've seen on Github. Well, there's a setting for that too!

Git VSCode Diff Settings side-by-side

Committing with the Sidebar
For a long time, I didn't know you could do this right from the sidebar. It's pretty slick. Perhaps you're like me and you skimmed right over the text in the box, thinking it was just a regular search. Check it:

Git VSCode Commit Sidebar

Type your commit message into this box, hit Cmd+Enter and presto! You've made a commit. The first time you use this, you'll likely get a prompt about staging changes. You can select Always on that prompt to stage all changes when you make a commit, so everything is automatic. Committing this way is roughly equivalent to the gac alias listed above.

Resolving Merge Conflicts
In my opinion, the merge conflict resolution is the best part of the editor's git integration. Maybe you avoid merge conflicts like that aunt who always asks you to help with her computer. With VS Code's merge conflict tools, you'll avoid conflicts no longer. Who knows, you might even enjoy resolving them! The magic is in those little links at the top. Accept Current Change, Accept Incoming Changes, and Accept Both Changes are the ones that I use most often. Click them and be amazed that you ever did it manually.

Git VSCode Merge Conflict

Better Interaction with Github

Now that you've improved how you work with git on your machine let's talk about the other task that we do all the time — interacting with Github. I used to have a handful of extra aliases to do things like open the repository on Github or open up the "create a pull request" page. Nowadays, though, I'd recommend the official Github CLI. You can use it to manage issues, pull requests, releases, and even entire repositories! I suggest you read the manual for the CLI tool to see what else it can do, but here's a quick overview of how to get started after you install it.

Setup

Log in with Github
Enter the following command, and follow the prompts to authorize with your Github account. I'd recommend choosing the SSH protocol over HTTPS. You never have to type passwords this way.

gh auth login

Set your default editor
Maybe you already use nano (that was the default for me), but I prefer to use vscode or vim. You can let the CLI know about this by running the following command:

gh config set editor code

Example Commands

Clone a repo
Maybe you have the github url structure memorized like me, but even then, this is shorter.

gh repo clone livewire/livewire

Do Anything with Pull Requests or Issues
You can accomplish pretty much anything you can do with the Github web app. I particularly like the pr and issue commands. Read the CLI docs to see the comprehensive list. There's tons of stuff.

gh pr list
gh issue list

Git Github Command PR Example

View the Repo on the Web
Pass the --web or -w flag to most commands to view the result in your browser. What a great shortcut! Try it with the various pr or issue commands to get even more bang for your buck.

gh repo view -w

See the Latest Releases
Enter this command when in a repository folder and quickly get release information. Nice time saver.

gh release list

Alias Extra Workflows
Finally, there's one other CLI command that is worth mentioning — alias. Use the alias command to make your own mashups of gh commands and further improve your workflow. The documentation on the CLI website isn't great, so run gh alias set --help to see examples and learn about the options available.

Here's an example to list all the issues with the bug label:

gh alias set bugs 'issue list --label="bug"'

You can use the --shell or -s flag for more power, like defining parameters. This one creates an issue with a title like To Do: {your title here} and a body to match.

gh alias set -s todo 'gh issue create --title="To Do: $1" --body="$1"'

Use it like so: gh todo "thing that needs to be done". Create your own and get creative!

If this article was helpful to you, follow me on twitter and sign up for the newsletter (below), where I share lots of great tips and resources for becoming a better developer. Thanks for reading! ✌️

Enjoy this article? Follow me on Twitter for more tips, articles and links.
😢 Awww, nobody has liked or mentioned this on Twitter yet.

Join the Newsletter ❤️

A most excellent monthly newsletter with code & design tips, curated links and more!
Don't worry, I'll never send you spam. Unsubscribe at any time.