How to start cooking with Git (Meet Git #4)

Create a Git repository and keep recording how your project evolves

Krystian Szpiczakowski
CodeX
7 min readOct 27, 2022

--

Photo by Jimi Filipovski on Unsplash

Hello and welcome to another article from the Meet Git series.

In the previous article¹, I was showing you how to go through Git initial setup. Today, as we’re good to go, we can finally do something cool, for example… write a cookbook.

Why cookbook? Well, I was hungry when I started thinking about making this series.

More serious, I could’ve written yet another article with some snippets of source code, but the thing is, as I mentioned in Meet Git #1, I wanted to go a little beyond of programming, and demonstrate you that version control systems (for example Git) are also fantastic tools in other fields apart from computer science.

Let’s get started!

Prerequisites

  • Git installed¹ and configured² on your computer (if you haven’t read my previous articles yet, check them out!)
  • A pinch of imagination, as we’re going to create a royal breakfast!

Use case: Creating a cookbook using Git

The general idea is to write a few recipes, experiment with them, and let Git manage all changes that we’ll be applying to our cookbook.

Oh, one more thing: we’re going to write down our recipes in the plain .txt format, so forget about LibreOffice Writer or Microsoft Word.

Why to write a cookbook in the .txt file?

I know, real cookbooks, like other books, are more complex than just a plain text.

Most often (if not always), books contain chapter names, paragraphs, sidenotes, and other sections stylized differently than a regular text. Even the content of the chapters itself has parameters, such as the appropriate size and type of font.

Because we’ll be writing down the recipe in the .txt file, we won’t be able to use the above attributes. You may think of it as a limitation, but in my opinion this approach has significant advantages:

  • Foremost, I want to show you how Git operates on simple text files, as Git really excels at it.
  • Once you understand the basics of Git, it’ll be much easier for you to understand why .doc, .docx and other common text formats are so special for Git (and why they’re tricky in terms of tracking changes)
  • Then you’ll be able to pick the right decision on how and what to store in Git repository

You may ask: Does this mean that I can use Git solely for keeping history of plain .txt files?

My answer is: Not at all, but you need to be aware of certain pitfalls.

For now, let’s just stick to the .txt format, and in the next articles we’ll be upgrading our cookbook. At some point, we will consider whether there is a more suitable text format to represent the cookbook.

Writing the first recipe

Create a git repository

Okay, I already got hungry, so there’s no time for theory.

First, we need to choose the directory where we’ll be working on our first recipe (this location will also be our Git repository). Open Git Bash or your favorite terminal, and go to the directory of your choice. In my case, I chose the Documents directory of my Windows user.

I’m going to the Documents directory

If you’re using Git Bash, there’s a high chance that Git Bash has started in the user’s home directory. How to know that? The ~ character stands for the user’s directory.

Here’s a short cheat sheet how to navigate in command line:

  • If you want to know where exactly you are, enter pwd, and you’ll see the absolute path to your current directory
  • To list the content of the current directory, enter ls command
  • To change the current directory, use cd command followed by the path you want to go to (if you are not comfortable navigating directories from the command line, you can find great tutorials on the Web, like this one³)

Alright, let’s create an empty repository. Enter git init cookbook, and this will tell Git to create a new subdirectory called cookbook:

An empty repository created by Git

You may wonder why you couldn’t just create this directory manually. As a reminder: a Git repository is a directory that contains a hidden .git directory inside. By entering git init command, Git did all the magic and prepared its .git directory accordingly.

About the status of Git repository

If you’re using Git Bash and you see the blue text saying master or main, you can be sure you are inside the Git repository. On the other hand, if you’re using a terminal on Linux, you may not see the current branch by default.

Git Bash shows the current branch
By default, Linux terminals don’t show Git-related information

Generally speaking, master and main are names of default branches in Git, but you don’t need to know what branch is at this moment.

The first operation we can perform is to check the repository status. Your repository is supposed to be empty — entergit status, and you should see something like this:

Git saying the cookbook repository is empty

Write the recipe

It’s time to write the recipe for our Royal Breakfast! Create a new file called royal_breakfast.txtin the cookbook directory, and write the following:

Ingredients:

- 1 roll

- cream cheese

- sprouts

Preparation:

Cut the roll in half, spread cream cheese on the roll and lay out the sprouts.

Write the recipe in your favorite text editor

Check the status of your repository

Once you saved the file, enter git status in terminal, and you’ll notice that Git found your recipe, but the file is not tracked by Git yet.

Git has noticed the new file, but it doesn’t track it yet

Select your file for next commit

To make Git track your recipe, enter git add royal_breakfast.txt, which will add the recipe to the staging area. By marking files as staged, you’re instructing Git which changes you want to store in the next snapshot (during the next commit).

We told Git to select our recipe for next commit

Tell Git to save your version of the recipe

Eventually, we can tell Git to store our precious recipe. You have two options to do this:

  • Enter git commit -m "<your_commit message>" (preferable method for short messages/one-liners)
  • Enter git commit, and then write the commit message in an opened text editor (preferable method for longer messages). Also, this option utilizes the text editor pointed in Git’s configuration²

If you see an error message saying something about missing username or email address, see my previous article², where I explained how to configure these settings.

If you configured it properly, you should see the screen as follows:

Creating a first commit in Git repository

Congrats, you’ve just performed your first commit in Git! But what exactly does it mean?

Your version of the breakfast is now stored and maintained by Git, which means whenever you change the contents of the royal_breakfast.txt file, or you even delete this file, Git will notice this, and you can then either save (commit) the new version on top of the previous commit, or you can restore the file to the state represented by your last commit.

Check the repository status after commit

Optionally, you can check the repository status with git status after you committed the recipe. This time Git tells you there’s nothing to commit, and this is because when you performed the commit, the staging area has been emptied, so it’s ready for another round.

By another round, I mean:

  • editing the recipe
  • staging changes (selecting what changes are going to be committed)
  • committing changes
After commit, “git status” shows the staging area is clean and ready for next round

Show the history of your recipe

In certain situations, we would like to know how and when our project was changing. The answer for when is git log.

Every commit contains information such as:

  • Author (Krystian Szpiczakowski)
  • Creation date (Thursday, October 27th 2022)
  • Message (Write a draft version of the Royal Breakfast)
  • SHA-1 checksum (1033ca3420…), where usually only first five characters matter

The checksum is an ID of each commit, which is crucial during working with Git, but you’ll learn about it later on.

“git log” shows the history of the repository

What’s next?

In the next episode, we will upgrade the Royal Breakfast to make it truly royal. Also, we’ll take a closer look at commits and how they relate to each other.

References

[1] Krystian Szpiczakowski, Install Git and start using it like a Pro (Meet Git #2) https://medium.com/@kszpiczakowski/install-git-and-start-using-it-like-a-pro-meet-git-2-b3c2dd96942f

[2] Krystian Szpiczakowski, Configure Git like a Pro (Meet Git #3) https://medium.com/@kszpiczakowski/configure-git-like-a-pro-meet-git-3-e48f82b1c346

[3] Marshall Gunner, How to Change Directories in Command Prompt on Windows 10 https://www.howtogeek.com/659411/how-to-change-directories-in-command-prompt-on-windows-10/

--

--

Krystian Szpiczakowski
CodeX
Writer for

The dev who enjoys writing tests / Home-grown barista