Basics of git

I've been a user of git for a long time and feel comfortable with it during every day use. Not everyone feels the same ways as git can be fairly intimidating, especially when used via the command line. Understanding what common git commands do can help even if you prefer to use it via GUI. First of all, knowing a bit about git can help shed light on how it works. Let's recap on git a bit!

What is git?

To summarize the official documentation: Git is a version control system (VCS), which helps you store data, along with it change history. Git is a snapshot based VCS, which means with each commit it stores a copy of all the files it tracks. Your first commit will store a copy of all your files, while later commits will will only make copies of changed files. If a file was unchanged, git will store a reference to the file from the last commit where it was changed. This avoids storing copies of unchanged files with each commit.

Git handles most of it's operations on your local machine, allowing it to have high performance. This means you don't need an internet connection to work & commit changes locally.

Collaborative work in git happens in remote repositories. These are remote copies of a git database that can be cloned to your local machine. Cloning downloads all files tracked in git to your machine so you can work locally, create commits and push them to the remote repository.

Cloning a project creates a working tree, which is a single checked of one version of the project

What data is stored in git?

As explained above, git initially stores a full snapshot of files it tracks, then snapshots of modified files. The files related to git are stored in a .git folder in the reposity's root folder by default.

Git keeps track of changes by generating SHA-1 checksums of the files that are tracked by it. This means that it has an easy way of tracking what files changes and it will notice even minor changes to files in it's database.

Git normally only adds data to it's database. Even if parts or all of a file is delete, data will be added to git to record the change. If a file was once part of git, it is hard to erase it from git. This means that it is not a problem if a tracked file is accidentally deleted, since git stores a snapshot of it in the version history.

Data tracked by git can reside in three states:

  • Modified files have been changed but are not committed to your database.
  • Staged files are modified and will be included in your next commit.
  • Committed files are committed and are stored in your git commit history.
States of data in git

The basic git workflow

While git has a lot of functionality, a basic workflow involves three things:

  1. You modify a file in the working tree.
  2. You add the changes you want to include in your next commit.
  3. You commit your stage changes. This adds them permanently to your git directory.
  4. (Optional) You push the committed changes to the remote repository.

Basic git commands

You may prefer to use a GUI git client to handle your version control, but these apps are a wrapper around the git CLI. Understanding basic commands or actions in git can help you with your workflow even in GUI git applications.

Please note, the following commands apply to the current working directory you are currently in via terminal or command prompt. Before you start, make sure your working directory is correct by calling:

cd /path/to/your/directory

git config

The config command allows you get and set configuration variables. To see all git related settings, you can use

git config --list --show-origin

You can set global settings, such as the email & name associated with your local machine. These will be used to annotate the commits that are made with your computer.

git config --global user.name "Ben Example"
git config --global user.email ben@example.com

git init

Creates an git repository in the current folder. This can be called in any folder, either empty or one already containing files.

git init

git clone

Can be used to clone a remote repository to your local machine. This command will create a local folder for the remote repo, download all files &

git clone {remote_repo_url}

git remote

Can be used to connect/remove a remote to your local repo. You will need to create a

git remote add origin {remote_repo_url}

git branch

Git branch can be used to list local branches for the current git repository. Adding the -a flag will also list remote branches not yet checked out locally.

git branch -a

When passing the -d flag, the command can also be used to delete a local branch after it has been merged.

git branch -d {branch_name}

If you want to delete an unmerged local branch, you can pass the -D flag.

git branch -D {branch_name}

git checkout

Checkout can be used to switch branches in the local git repo.

git checkout {existing_branch_name}

It can also be used to create a new branch from your currently checked out branch.

git checkout -b {new_branch_name}

git fetch

Fetching will download a remote branch and store it in a temporary FETCH_HEAD locally. This can then be used to perform a merge locally or to update your current branch to the latest remote code. This can also be done easier via running git pull.

git fetch origin {branch}

git pull

Git pull will perform a fetch to get the latest code for the branch from the remot and follow it up by a merge to the current branch. This will effectively update your local files to what is contained in the remote. This command will throw an error if you have uncommitted files locally.

git pull

git commit

Creating a commit will create a snapshot from the locally staged changes. This means the changes you made will be "saved" to git locally for safekeeping. Make sure to push changes after this!

Passing the -am flag lets you specify a commit message string, within single quotes.

git commit -am 'commit message'

git push

Pushing will upload your current working tree to the remote (if one is configured).

git push

git merge

Merging allows you to merge a remote branch into your current branch.

git merge {branch_to_merge_into_current}

Terminology

repository
A repository or repo is a standalone git database and all files tracked by git. Cloning a repository is the first step in starting collaboration on an existing project.

working tree
A single checkout of one version of the project. Basically a copy of all files in the git repository for the commit & branch that has been checked out.

commit
The action of taking a snapshot of the files in the working tree.

remote
Refers to a cloud hosted version of a git repository, usually stored on Github, Gitlab, Bitbucket, Azure Devops or your own self-hosted git server. When you push, you upload files to the remote repository, while pulling will download files from the remote to your local machine.

Ben Zatrok

Ben Zatrok

Netherlands