eldelto
Created:

Get Good at Git - Getting Started

In this little series of articles we will cover everything you need to know about Git (our versioning weapon of choice) to efficiently manage your software project's changes without losing any of them ever again.

About

First of all, what even is Git and why should we care?

Git is a version control system (VCS) for software projects (but we can really use it for anything we like) that enables us to store a full history of all your code changes and collaborate with multiple people on the same project in a structured manner.

Gone are the days of manual backups with names like awesome-project_v2 or bitcoin-miner_final-version-for-real (been there, done that) but a sane way to store your project with the abilitiy to figure out what changed at any given time.

Let's dive in.

Installation

When starting out we need to make sure to have a recent version of the git command line tool (CLI) installed. It comes pre-installed on most operating systems but let's fire up a terminal and check ourselves:

          git -v

          # Output
          git version 2.41.0

In case of any error please consult your OS's package manager on how to install Git or visit the git-scm.com downloads page and follow their installation instructions.

Getting Help

Git comes with pretty extensive built-in documentation that can be navigated using the git help subcommand. Running git help without any arguments will list the most popular commands with a short description of what they do.

git help -a show a list of all available commands and git help -g will print all available concept guides, which are basically built-in tutorials about various topics.

We can get mroe detailed information about any specific command or concept guide by passing it as argument to the help command: git help <command>. For example git help status will tell us more about the add command and git help tutorial will display a written tutorial.

Config System

Before diving into the meat and potatoes of everything we should set our user name and E-mail adress as those will be referenced throughout various Git operations.

To do this we need to examine Git's config system a bit with the help of the git config command.

The idea behind it is that one can assign values to keys. Git will look at the values of certain keys when executing a command and will use the configured value accordingly. For example the value of the key user.name will be used in various commands to identify the author of a code change.

We can list all keys and their values that are currently applied by running git config --list:

          git config --list

          # Output
          core.excludesfile=/Users/eldelto/.gitignore
          core.editor=vim
          pull.rebase=false
          init.defaultbranch=main
          ...

Git's config files come in multiple tiers:

  • system - Applies to every user on the system; usually located at /etc/gitconfig
  • global - Applies to all projects of a single user; usually found at $HOME/.gitconfig
  • local - Applies only to the current Git repository; located at <project>.gitconfig

All config commands take the target config level as optional flag (defaulting to local, except for --list which shows every config that applies to the current location). For example running git config --list --local will only display config entries that are configured within our current project.

Fortunately there is also no need to edit those files manually as Git provides us with a mechanism to add, remove and inspect individual values. Let's give those a try.

We can set a single value by using git config <key> <value>.

On the other hand fetching a single value can be achieved with git config --get <key>.

Let's set the value of key test.key1 to hello and fetch it immediately afterwards for verification:

          git config --global test.key1 hello
          git config --global --get test.key1

          # Output
          hello

This brings us to our last operation so we can cleanup after ourselves - git config --unset <key>. This command will delete the config entry with the given key.

          git config --global --unset test.key1
          git config --global --get test.key1

With the basic Git config commands under our belts, we can finally set our user name & E-mail adress globally:

          git config --global user.name <user-name>
          git config --global user.email <e-mail>

And quickly verify that it actually worked:

          git config --list --global

          # Output
          user.name=eldelto
          user.email=eldelto77@gmail.com
          ...

Project Initialization

Now that we are really ready to go let's create a new directory called git-playground, change into it and initialize an empty Git repository inside of it by running git init:

          mkdir git-playground
          cd git-playground
          git init

          # Output
          Initialized empty Git repository in .../git-playground/.git/

A new .git directory has been created within our project that is used by Git to track code changes. We can take a look inside any time with

          ls -la .git

          # Output
          total 24
          drwxr-xr-x   9 eldelto  staff   288B Nov 12 21:25 ./
          drwxr-xr-x   3 eldelto  staff    96B Nov 12 21:25 ../
          -rw-r--r--   1 eldelto  staff    21B Nov 12 21:25 HEAD
          -rw-r--r--   1 eldelto  staff   137B Nov 12 21:25 config
          -rw-r--r--   1 eldelto  staff    73B Nov 12 21:25 description
          drwxr-xr-x  16 eldelto  staff   512B Nov 12 21:25 hooks/
          drwxr-xr-x   3 eldelto  staff    96B Nov 12 21:25 info/
          drwxr-xr-x   4 eldelto  staff   128B Nov 12 21:25 objects/
          drwxr-xr-x   4 eldelto  staff   128B Nov 12 21:25 refs/

For our uses we can just ignore this directory, but make sure to not remove it by accident as this will delete your local Git history.

We can now run git status to view the current status (the audience gasps) of our project.

          git status

          # Output
          On branch main

          No commits yet

          nothing to commit (create/copy files and use "git add" to track)

This tells us the current branch/commit we're on (more on that in a later article) and that there are no changes (nothing to commit).

Now we are all set to create the first changes in our project! Buuut... this will have to wait until the next article.

(What a cliff-hanger...)