Basics

We will learn some basic commands to get to know git a little bit better.

git configuration

In order to track contributions in a project we set our user name and our email address. Your name and email will be associate with your commits.

Exercise - git user name

Set up your git user name:

git config --global user.name "first-name last-name"

Replace the placeholders with your first-name and last-name.

Exercise - git user email

Set up your git user email:

git config --global user.email "your-name@company.de"

Replace the placeholder with your email address. Some Git hosting platforms (e.g., GitHub, GitLab) use this to link commits to your account.

Exercise -default text editor

Set your default text editor to Nano:

git config --global core.editor "nano"

This defines which text editor git should use when opening commit messages or rebase instructions. With your favorite text editor set you can edit messages easily if git prompts you for input. Other options are e.g. Vim or VS Code.

Exercise - verification

Now let’s verify that all settings are configured correctly by running:

git config --list --show-origin

Solution

You should get an output similar to the following:

$ git config --list --show-origin
file:/Users/your-user/.gitconfig      user.name=first-name last-name
file:/Users/your-user/.gitconfig      user.email=your-name@company.de
file:/Users/your-user/.gitconfig      core.editor=nano

Working with git

We will now have an easy start with git by creating our first repository and our first revision.

Exercise - local directory

First create a local directory on your machine:

mkdir git-beginners

You can check if the creation of the directory was successful with the ’ls’ command:

ls

Solution

The output of the ’ls’ command should look like this:

$ ls
git-beginners

It is possible that there are other directories or files shown. Don’t worry unless you have the git-beginners directory listed.

Now navigate to the ‘git-beginners’ directory you’ve just created:

cd git-beginners

Check if you’re in the directory by executing ‘pwd’:

pwd

The ‘pwd’ command displays the directory you’re currently in.

Solution

Your output should look similar to this:

$ pwd
/home/novatec/git-beginners

Finally we will initialize a new git repository with ‘main’ as the initial branch name:

git init --initial-branch=main

This marks our git-beginners directory as a git repository and sets up the necessary metadata so git can start tracking changes.

Solution

You should see the following output after executing the command:

$ git init --initial-branch=main
Initialized empty Git repository in /home/novatec/git-beginners/.git/

You can also check for the ‘.git’ folder using the following command:

ls -lah

This command will list all the folders and files in your directory (also the hidden ones):

$ ls -lah
total 0
drwxr-xr-x   3 your-user  staff    96B Mar 18 12:14 .
drwxr-x---+ 42 your-user  staff   1.3K Mar 18 12:06 ..
drwxr-xr-x   9 your-user  staff   288B Mar 18 12:14 .git

The ‘.git’ folder contains all the repository metadata, including commit history, branches, and configuration files.

Exercise - revision

Next we will create our first revision for our repository.

Execute the following command to create your first revision:

echo “My first revision“ >> <your-name>-first-revision.txt

Again with the ’ls’ command you can verify that the file was created.

Solution

The output of the ’ls’ command should look like this:

$ ls
  your-name-first-revision.txt

Now use the ‘add’ command to move your new file from the working directory to the staging area, preparing it for commit.

git add your-name-first-revision.txt

With the ‘status’ command you can verify that the file was added:

git status

Solution
$ git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
 new file:   your-name-first-revision.txt

Finally we ‘commit’ our file and save it from the staging area to the repository’s history.

git commit --message '<your-name> initial commit'

Solution

Expected output from the previous command:

$ git commit --message '<your-name> initial commit'
[main (root-commit) 6e4c49d] your-name initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 your-name-first-revision.txt

Optional: Useful git aliases - making future work smoother

To make git commands more efficient, set up aliases:

git config --global alias.logo "log --oneline"
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm "commit -m"

Verify alias settings:

git config --global --list | grep alias

Instead of using git log --oneline you could now just use your alias git logo.


You are now ready to proceed with the poem exercise The poem - Part 1 .