Codementor Events

Git 101 - introduction

Published Jun 10, 2019
Git 101 - introduction

This article will be about getting started with Git, what the basic and most used commands are. Note: this article assumes that you have Git installed and know the basic of working with a terminal.

Setting up your repository

Make sure you're in a directory where you want to create your repository.

mkdir git-101
cd git-101
git init

After doing this you should see the following message:

Initialized empty Git repository in [pwd]/git-101/.git/

You've now told Git that it should start watching this directory for changes and we can proceed with the next step.

Creating your first commit

Now that the repository has been setup we can continue with making your first commit.

Create a new file with your text editor of choice and save it in the directory that you've just created your Git repository in. I will make a file called: greetings.txt and enter the following line in it:

Hello world!

Now, lets see what Git thinks of this, enter the following command:

git status

Your output should be as follows:

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

  greetings.txt

nothing added to commit but untracked files present (use "git add" to track)

Git will actually tell you that there are untracked files. What this means is that we haven't toll Git to track these files yet, so lets do so with the following command:

git add greetings.txt

And if you request the status again with git status, you should now get the following output:

On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

  new file:   greetings.txt

The file has now been staged and is ready to be committed. Committing a file in Git is as easy as one simple command:

git commit

Typing this will actually open up the default text editor that you use in your terminal. There's also an even easier way to make a commit with the -m flag, this will allow you to skip opening up your text editor altogether, I personally use this when I make a simple commit where I'm able to fit the description of the commit on one line:

git commit -m "chore: added greetings.txt"

After making the commit (with or without the -m flag), you will see the following:

[master (root-commit) e53da7b] chore: added greetings.txt
 1 file changed, 1 insertion(+)
 create mode 100644 greetings.txt

Git has now made the commit and will given you a quick summary of the files that have been changed and the number of inserts and/or deletions.

You can view the commit that you just made by typing:

git log
commit e53da7b689879136a24eb78b44abada4b77667c7 (HEAD -> master)
Author: Your Name <your-email@provider.com>
Date:   Sat Jun 8 12:46:03 2019 +0200

    chore: added greetings.txt

Be sure to visit my profile more often, I'll be making more posts about Git and various other subjects.

Discover and read more posts from Kevin Visscher
get started