How to use the git .gitconfig file for a more efficient workflow

GitHub logo on the screen of smartphone and notebook close-up.
Image: prima91/Adobe Stock

Git is the most widely used version control system on the market. It’s powerful, flexible, and makes collaborating on development projects a breeze, but one thing you might have come across is that you have to re-run certain configuration commands for each initialized repository.

SEE: Preference Kit: Python Developers (TechRepublic Premium)

For example, you have initialized the PROJECT directory, and then you need to configure your username and email address with commands like the following, which run in the PROJECT directory:

git config user.email "[email protected]"
git config user.name "Jack Wallen"

Or you could use the –global option, which as the name suggests is global. These commands would look like this:

git config --global user.email "[email protected]"
git config --global user.name "Jack Wallen"

The difference between the two is that the first set of commands run on a project basis while the second set of commands are used to set global configuration options. The latter option is more efficient as it ensures that you don’t have to run these commands for every project you work with.

If you use the –global option, a configuration file named .gitconfig will be generated in your HOME directory. In this file you can set several important configuration options.

How to open the .gitconfig file

There are two ways to open the .gitconfig file. First, you can use the git command like this:

git config --global --edit

This will open your .gitconfig file in your default text editor.

You can also open the file directly with the command:

nano ~/.gitconfig

The content of this file looks something like this:


[user]
        email = [email protected]
        name = Jack Wallen

[cola]
        spellcheck = false
        maxrecent = 6

[gui]
        editor = gedit

[color]
        ui = auto

[filter "lfs"]
        required = true
        clean = git-lfs clean -- %f
        smudge = git-lfs smudge -- %f
        process = git-lfs filter-process

Let’s take a look at some of the handy additions you can make to this file.

The first addition you need to make is setting your default branch. Let’s say your default branch is main. To set this, you would add the following below:

[init]
        defaultBranch = main

Add the below, making sure to follow the same indentation as set in the file.

Next, let’s add your default text editor with:

[core]
        editor = nano

Suppose you work with certain repositories throughout the day and don’t want to have to retype the addresses every time. You can create shortcuts in .gitconfig like this:

[url "https://github.com/"]
        insteadOf = gh:

[url "https://gist.github.com/"]
        insteadOf = gist:

[url "https://bitbucket.org/"]
        insteadOf = bb:

You may also want to enable colored output. For example, you can set different colors for things like branching, diff, and status. Here is an example:

[color]
        ui = 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

You can also add aliases for commands to make your work even more efficient. Aliases in the .gitconfig file look like this:

[alias]
        # Show all branches
        br = branch -av 
        # Show the current branch name (useful for shell prompts) 
        brname = !git branch | grep "^*" | awk '{ print $2 }' 
        # Delete a branch brdel = branch -D

You can also set your default web browser like this:

[web]
        browser = firefox

Or maybe you need to use Gmail outgoing server settings like this:

[sendemail]
        smtpencryption = tls
        smtpserver = smtp.gmail.com
        smtpuser = EMAIL
        smtppass = PASSWORD
        smtpserverport = 587

Where EMAIL is your Gmail address and PASSWORD is an app password you create.

Finally, you can also set your Github credentials like this:

[github]
        user = USERNAME
        token = TOKEN

Where USERNAME is your GitHub username and TOKEN is an authentication token that you generated in the GitHub security settings.

After adding your options, save and close the file. You can then view the contents of this file with the following command:

git config --list --show-origin

Not only should you see all of your entries, you should also see the path of the file that contains them, namely file:/home/USER/.gitconfig, where USER is your username.

And that’s your introduction to the .gitconfig file. If you’ve ever wanted to know exactly how Git is configured on your machine, you know you have such power at your fingertips.

For more information on configuring Git, see the official documentation.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for the latest tech advice for business professionals from Jack Wallen.

Leave a Reply

Your email address will not be published. Required fields are marked *