DEV Community

Asaju Enitan
Asaju Enitan

Posted on • Updated on

Configuring Github on your Dev Environment Part One

Configuring Github on your Dev Environment Part One

$ means running the command as a normal user but make sure to remove it from your command as it already exists on your terminal

Setup

The first step in setting up git on your dev environment is installing the git to your environment. Although you can use Git with the web version with https://github.com, most of the time you will be interacting with Github from within your dev environment using either Terminal and/or applications.
First go to https://git-scm.com/ and install for your OS.
For Windows user, using the installer will also install the git bash application which you can use to run git commands.

Linux users will have to download for their distribution https://git-scm.com/download/linux

While MacOS users can use Homebrew to install by running brew install git. Make sure to install Homebrew if you don't have it already.

If you like living on the edge, you can build from source with the tarballs from kernel.org.

Basic Configuration

After going through the installation process for your OS, you will need to make configurations. But before that, you should make sure that git installed successfully on your environment. For Windows users, during installation, you will be asked if you want to install to path, if you didn't check the box, you will need to add it to your environment variables.

Git comes with a tool called git config that lets add and read configuration variables that control how you use Git. These variables can be saved in three different places;

  • /etc/gitconfig file: The variables saved in this location applies to every user on your system and all the repositories. To save configuration variables to this file you must pass --system flag to the git config command. You will need administrative or superuser privilege to use this command because it writes to a system file.
  • $HOME/.gitconfig or $HOME/.config/git/config file: The configuration variables saved to this file apply only to a single user, You. You can save configuration variables to this file by passing --global flag to the git config command. The variables in this file affects only the repositories that you manage.
  • config file in the Git directory i.e .git/ in your project directory. To know if you have this directory in your project folder, you can run ls -al which displays all the hidden files/folders in your project, you can also access see this by checking Show hidden files in your OS's file explorer. Now that you know where to store files. Let's add some configuration variables. You will be going with the second option because you will want to apply to yourself and all the repositories you own.

Setting Your Identity

The first thing you should do when you install Git is to set your user name and email address. Git uses this to bind every commit towards you. This is important so that you will be able to keep track of every commit you make.
Open your preferred terminal and type the following;
$ git config --global user.name "John Happer" This command sets the user name. Replace the John Happer with your full name.

$ git config --global user.email "johnhapper@example.com" This commands sets the email address. Make sure you replace johnhapper@example.com with the email you registered your github account with. Using the --global flag makes sure to set these variables to only you but affects all the repositories.

Editor

You have set up your identity, you can configure the default text editor that will be used when you need to type messages or make amends to commits. It is not a must though as Git will make use of your system's default editor.

But if you want to use a different editor, for example VSCode, you can run the command like this
$ git config --global core.editor code. This will use the VSCode executable code. For windows users, you will need to specify the full path to the editor you want to use. For example, if you want to use VSCode on windows, you will need to type something like this;

$ git config --global core.editor "C:/Program Files/Visual Studio Code/code.exe"

Make sure you get the correct path as this can cause issues when Git attempts to open the executable.

Default Branch

By default, when you run git init to initialize a new repository for your project, Git will create a branch called master. But you can set a different name for the initial branch. For example to set main as the default branch name, type
$ git config --global init.defaultBranch main and it will set main as your default initial branch every time you run git init.

Phew!, you have done a lot. To check the current variables you have set, you can use the git config --list to show the list of everything Git has set. This command reads from all the Git config files.

$ git config --list
user.name=John Happer
user.email=johnhapper@example.com
core.editor=vim
color.status=auto
color.branch=auto
...
Enter fullscreen mode Exit fullscreen mode

... The rest of the variables have been truncated.
At any point if you forgot a command, you can run $ git help <verb> to get a comprehensive manual for that command. For example you can get the documentation for git commit by running $ git help config. For short and concise help options, you can use the $ git <verb> -h for example $ git add -h to get help for the github add command.

usage: git add [<options>] [--] <pathspec>...

    -n, --dry-run         dry run
    -v, --verbose         be verbose

    -i, --interactive     interactive picking
    -p, --patch           select hunks interactively
    -e, --edit            edit current diff and apply
    -f, --force           allow adding otherwise ignored files
    -u, --update          update tracked files
    --renormalize         renormalize EOL of tracked files (implies -u)
    -N, --intent-to-add   record only the fact that the path will be added later
    -A, --all             add changes from all tracked and untracked files
    --ignore-removal      ignore paths removed in the working tree (same as --no-all)
    --refresh             don't add, only refresh the index
    --ignore-errors       just skip files which cannot be added because of errors
    --ignore-missing      check if - even missing - files are ignored in dry run
    --chmod (+|-)x        override the executable bit of the listed files
    --pathspec-from-file <file>
                          read pathspec from file
    --pathspec-file-nul   with --pathspec-from-file, pathspec elements are separated with NUL character
Enter fullscreen mode Exit fullscreen mode

And that's all about basic setup for your github. Next up, we will look at setting up your git commit messages.

Adding Files

You have made changes to your code, and now you need to push to the remote repository. To do that, you will use the git add command. You can add files in several ways. Running git add . will add all modified and new files and stage them for commit and push. But you can also specify which file you want to add by running git add path/to/file/in/the/repositiry i.e git add app/controllers/user.js will add only the user.js file and stage it.

Removing Files

You can also remove files i.e unstage them from commits by running git rm path/to/file/in/the/repository. The git rm . also unstages all files and folders. Not to be confused with git stash, this command clears the current commit and reverts to the last push commit in the log which can be accessed using the git log command.

Commit Messages

Commit messages are a way to keep note tracks of your project updates. It is very important because it helps when you revisit the codebase or share the repository with someone else. It is very important to make sure your commit messages as descriptive as possible. Avoid short messages such as updated app.js, the change here is now working, these kind of messages leave little space for understanding your code and can waste productive time.

That being said, how should you write a good commit message. A good commit message needs to follow a good idea of what the update is about, a summary or it and full text explaining to an extent what the update to the commit is.

An example of this is <scope> <summary> <full text>.

Scope

Where <scope> is the feature/function of the commit, i.e is it a feature, a test, a breaking change or a fix.
A way you can do this is using keywords, these keywords has to be descriptive enough, listed below are some examples;

  • feat
  • fix
  • breaking changes
  • new
  • improve
  • refactor, etc...

Summary

Where <summary> shows the TL;DR version of the commit. For example, what function was refactored or what issue was resolved. It is advisable to keep this at 20 words max as you will still give a detailed explanation in the <full text>. Example might be, Added validator to the create user endpoint.

Full Text

The <full text contains the full detailed explanation of the explanation, will it affect any other commit?, is there a command that must be run for it work?. You can type as much as you want here since it will help you or anyone checking the code to grasp a full explanation of the commit.

So a full commit message will be something like $ git commit -m "UPDATE(Added validator to the create user endpoint): To avoid a security issue, user registration now requires the email to be of the email format, username must be more than 3 characters and password must be at least 8 characters. This is going to affect the registration endpoint which might generate error."

Alias

That being said, typing all these for every commit can be sometimes very tiring 😫. That being said, Git has a tool to exactly help with that and that is the git config --global alias🙂. With this command, you can set at much as aliases as you want. Let\'s set an alias to make an set a commit message for the initial commit.

git config --global alias.int '!f() { git commit -m "🎊 INITIAL COMMIT($1): $2; }; f' Running this command will add the alias command to your config file. Then when you run git int "Project Setup" "Setting up the base files, we will be using this technology etc..." it will translate to $ git commit -m "🎊 INITIAL COMMIT(Project Setup): Setting up the base files, we will be using this technology etc..." which looks cool and you get to type less.

You can add more of these alias and more with the git config --global alias.<keyword> command. I will share my config file so you can see how I did mine and edit according to your preference.

With this all set up, you are well on your way of becoming adept at using git. This is the first in the series and I will be updating as soon as I can.

Link to my config.

See you next time 😉❤️

Top comments (0)