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

Estimated read time 4 min read

[ad_1]

GitHub logo on the screen smartphone and notebook closeup.
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 snap, but one thing you may have come across is that you have to rerun certain config commands for each initialized repository.

SEE: Hiring kit: Python developer (TechRepublic Premium)

For example, you’ve initialized the directory PROJECT and then you’ll need to configure your username and email address with commands like this, run from within 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 implies, 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 is run on a project-by-project basis, whereas the second commands are used to set global configuration options. The latter option is more efficient because it ensures you don’t have to run those commands for every project you work with.

When using the –global option, it generates a configuration file within your HOME directory, named .gitconfig. That file is where 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 so:

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 contents of that file will look 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 to make is setting your default branch. Let’s say your default branch is main. To set that, you would add the following at the bottom:

[init]
        defaultBranch = main

Add that at the bottom, making sure you follow the same indentation as set in the file.

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

[core]
        editor = nano

Let’s say you tend to work with certain repositories throughout the day and you don’t want to have to type out the addresses every time. You can create shortcuts in .gitconfig like so:

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

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

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

You might also want to enable colored output. For example, you can set different colors for things like branch, diff, and status. Here’s 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 could 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 so:

[web]
        browser = firefox

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

[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’ve created.

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

[github]
        user = USERNAME
        token = TOKEN

Where USERNAME is your GitHub username and TOKEN is an authentication token you’ve generated from within the GitHub security settings.

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

git config --list --show-origin

You should not only see all of your entries but the path of the file that contains them, which will be file:/home/USER/.gitconfig, where USER is your username.

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

For more information on how to configure Git, make sure to check out the official documentation.

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

[ad_2]

Source link

You May Also Like

More From Author