Skip to content

Introduction to repositories

Git is a distributed version control system or DVCS.

The version control change history is stored in a repository.

The distributed nature means that you will always have a local repository, and potentially a remote one.

To get started, you will first have to initialize a repository.

We'll start with a local one, but working with others requires you to have something shared.

Initializing a repository

First hop into a directory where you are happy to be creating a new repository.

For example something like C:\git works on a Windows, or ~/git for Mac and Linux. But you're free to use any working directory you want!

cd <my git root>

Example:

$ cd /Users/username/Documents/git/work

You will probably have multiple repositories that you are working with, so create a new subdirectory and change to that:

mkdir my-first-repository
cd my-first-repository

Git needs some internal information, which it holds in a .git subdirectory. To set everything up, run git init in the directory above:

git init

Example:

$ mkdir my-first-repository
$ cd my-first-repository
$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty Git repository in /Users/username/Documents/git/work/my-first-repository/.git/

If you so which, you can also give the directory to create to the init command. But the above works from any existing directory as well! So it's easy to get started with Git on a project you have been working on for a while.

Example:

$ cd /Users/username/Documents/git/work
$ git init another-repository
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty Git repository in /Users/username/Documents/git/work/another-repository/.git/

Making your first commit

As visible in the example outputs above, the master branch is the current default. But it could be anything that suits you.

We are not concerned about branches just yet, so let's trust Git to do it's thing and create a file and commit it in.

Jump in to the repository you have initialized:

cd my-first-repository

Since we started from scratch, there's no files in the repository yet. Create one with our favourite editor.

Let's call it "my-first-version-controlled-file".

Type in some content, like "Hello world!" and save the file.

To get the file version controlled, it first needs to be added to a staging area:

git add my-first-version-controlled-file

You'll commonly want to check out what is the current state of your files in the repository. git status is a helpful command:

git status

After seeing everything is in order and there's one file to be added, you can commit the file with an accompanying message.

The messages are important, as they give details to others using the same repository about the change you are introducing.

You can also commit without setting the message directly with -m. Git should then open an editor and let you write the message there. Usually it's worthwhile keeping the messages short, so let's just set one from the command line directly and keep it simple:

git commit -m "Initial commit"

You can now type git log to see the history of your repository. It is not very interesting yet, but you can see the first commit:

git log

Example of committing a file:

$ git add my-first-version-controlled-file

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   my-first-version-controlled-file

$ git commit -m "Initial commit"
[master (root-commit) 8d1765a] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 my-first-version-controlled-file

$ git log
commit 8d1765ac9252d7bc6ce96bc54ecb685732ec2839 (HEAD -> master)
Author: Miika Kankare <miika.kankare@polarsquad.com>
Date:   Tue Apr 27 15:48:58 2021 +0300

    Initial commit

As you can see above, all commits get an ID. It's a fairly long hexadecimal string, but it's actually just a SHA checksum of the commit's contents along with some other information.

Luckily you don't have to remember these, git handles all of it internally!

Next

We could continue on committing more files to the default branch, but much of the power of Git comes from it's easy branching model.

Branches let you work on multiple things with multiple people simultaneously.

Let's create our first branch next.