git is a commandline version control tool.

It tracks how content changes and makes different changes happening at once easier to manage.

It offers a few key features:

  • diffing: comparing two versions
  • branching: working with different versions
  • merging: combining different versions into a new version

This guide uses graphs to establish a model of the git workflow. The internal workings of git are explored for a better understanding of how and why certain features work. It is not necessary to completely understand how it works internally to use git itself, but the hope is that it provides better clarity.


A Typical Workflow

?

git pull
git add .
git commit
git push

Installation

Getting the executable.

Git can be installed by following the official documentation: https://git-scm.com/install/. Installation makes the git command available by storing the binary executable in a particular folder.

PATH is a variable in operating systems that lists the folders to look for executables:

echo $PATH  # Linux and MacOS
path        # Windows

The exact location of the executable can be determined:

which git   # On Linux and MacOS
where git   # On Windows

If the folder is not in path - for example, by downloading the executable manually to a different folder - the full path to the executable must be used to run git, or that folder must be added to the path.

Running with full path:

/usr/bin/git --version

The Repository

New Repository

Starting tracking.

A repository is just a folder which has a .git sub folder.

A new repository can be initialized:

git init

This creates the hidden .git folder in the current directory and copies the contents of a template folder. The default template folder is /usr/share/git-core/templates 1.

The .git folder is particularly important to how git works. The git executable can be run from any directory, but it will search for the .git folder in the current tree to determine which repository it is working with.

Existing Repository

Resuming development.

An existing repository can be copied with:

git clone https://git.example.com/example/example.git

This creates a new directory example and copies the content (along with the .git directory) to this directory. A similar effect can be achieved by manually copying the repository, or using tools like scp or rsync, but git clone does a few extra git specific things:

  • creates remote tracking branches
  • creates and checks out initial branch

These actions are defined in the promotion model.

Internal Structure


Promotion Model

To go through each of these stages, a particular command is used.

git commit
git log --oneline
git reset --soft HEAD~1
git status
git add .
git restore --staged
git reset
git diff --cached
git config user.name "Name"
git config user.email "name@host.tld"
git commit -m "commit message"
git push origin master
git remote add origin https://git.example.com

Branching Model

git branch

Collaborative Workflow

git pull
git merge

Merge Conflict

Remotes

git remote