If you’ve ever worked on a project with me, you’ve probably heard me say “work smarter, not harder” before. This isn’t some gem I picked up from a supervisor when I was young, or a fancy presentation at a conference. It’s from an episode of DuckTales.

It’s a phrase that has stuck with me since I was a child in the 80’s, and is something I heard around the time I wrote my first line of code.

In the first post in Valiant’s series on Git, I mentioned that once you set your username and email with the git config command and used the —global switch, the values are saved in a .gitconfig file located in your home directory. The .gitconfig file contains configuration options that can make working with Git at the command line much easier.

A .gitconfig file can exist in up to 3 locations in your file system. The location of the file determines the scope in which the file’s contents are applied.

Global (~/.gitconfig)

This is the most common occurrence of the file, and it is used to set configuration options for a particular user.

System (/etc/.gitconfig)

This isn’t used terribly often; it sets the configuration for the entire local system. Most settings found in this file are overridden by a global .gitconfig file.

Local (/git-demo/.gitconfig)

This is the most localized place you can have a .gitconfig file as it only applies to the project it is located in. Keeping a .gitconfig file in a project isn’t a good practice – unless you need to enforce certain setting across all users involved in the project.

The purpose of this post is to make Git easier for you, the developer, so we’ll make changes to the global .gitconfig file.

Endpoint Shortcuts

If you work with several repositories on a daily basis, endpoint shortcuts can really come in handy. For instance, instead of typing the following to clone the repository we’ve used throughout these posts, we would type:

git clone https://github.com/ValiantTechnology/getting-started.git

Why type that when we can simply type this?

git clone gh:ValiantTechnology/getting-started

It doesn’t seem like much of a shortcut, but you’ll notice the difference if you are cloning repositories on a regular basis.

In order to take advantage of this endpoint shortcut, copy and paste the following in to your .gitconfig file:

[url “https://github.com/“]

    insteadOf = gh:

Of course, this isn’t limited to just GitHub. Endpoint shortcuts will work for any URL, so they’ll work for Bitbucket, Gitlab, and privately hosted repositories.

Colored Terminal Output

You may have realized that some of the screenshot in this series of posts are a bit more colorful than your own. Git will add a splash of color to its output messages if you provide some details in your .gitconfig file. Below are the settings that I’m using in my own .gitconfig file:

[color]
    ui = true
    diff = true
[color “branch”]
    current = yellow reverse
    local = yellow
    remote = green
 
[color “diff”]
    meta = yellow bold
    frag = magenta bold
    old = red bold
    new = green bold
 
[color “status”]
    added = yellow
    changed = green
    untracked = red

Git Aliases

I’ve always been one to use aliases wherever possible, and Git is no exception – although I’ll admit that it took some time for me to realize just how helpful they’d be. Alexander Ross a.k.a. @bleen gave a presentation on Git at the 2016 NYC Drupal Camp and shared a great set of aliases with everyone and I’ve used most since, with a minor edit here and there:

[alias]
     # View abbreviated SHA, description, and history graph of the latest 20 commits
     l = log –pretty=oneline -n 20 –graph –abbrev-commit
 
    # Log + diff
     lg = log -p
 
    # Log out loud
     lol = log –pretty=oneline –abbrev-commit –graph –decorate
 
    # Log when something isn’t quite right
    wtf = log –graph –pretty=format:’%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset’ –abbrev-commit –date=relative
 
    # Diff
    d = diff
 
    # Status
    s = status -s
    st = status -bs
 
    # Show all of my configured aliases
    aliases = !git config –list | grep ‘alias\\.’ | sed ‘s/alias\\.\\([^=]*\\)=\\(.*\\)/\\1\\ \t => \\2/’ | sort

The combination of the endpoint shortcuts, output colors, and aliases will give your Git command line skills a boost – more than a refreshing glass of Brawndo. What I have here is just scratching the surface though, so I suggest taking a look at your own workflows and see where a couple of minor changes can net a big result.

Remember, as Scrooge McDuck’s father said, “Work smarter, not harder.”

Related Posts