Make the Royal Breakfast truly royal (Meet Git #5)

How to track new versions of your project and compare differences

Krystian Szpiczakowski
CodeX
6 min readNov 6, 2022

--

Photo by Markus Spiske on Unsplash

Hello and welcome to the fifth(!) article of the series about Git not only for developers.

Previously¹, we created a new repository and saved an initial version of the Royal Breakfast. I agree, it’s hard to call that breakfast royal in the current state, but now we have a good base to experiment with.

Today, you’ll learn:

  • How to add new changes to the project
  • How to compare differences that will be committed
  • H̶o̶w̶ ̶t̶o̶ ̶r̶e̶a̶d̶ ̶t̶h̶e̶ ̶h̶i̶s̶t̶o̶r̶y̶ ̶o̶f̶ ̶t̶h̶e̶ ̶r̶e̶p̶o̶s̶i̶t̶o̶r̶y̶
  • H̶o̶w̶ ̶c̶o̶m̶m̶i̶t̶s̶ ̶a̶r̶e̶ ̶r̶e̶l̶a̶t̶e̶d̶ ̶t̶o̶ ̶t̶h̶e̶ ̶o̶t̶h̶e̶r̶ ̶o̶n̶e̶s̶

Add some goodies to breakfast

Check the repository status

At this point, our working directory should not contain any changes compared to the repository. If Git says there’s nothing to commit, and working tree is clean, we can move on.

The cookbook directory has no changes compared to the repository

Edit the recipe

Let’s add some vegetables apart from sprouts, for example avocado and tomatoes. The revised recipe looks as follows:

Ingredients:
- 1 roll
- cream cheese
- sprouts
- 1/2 of avocado
- 3–4 cherry tomatoes

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

Slice an avocado around the pit and halve it. Because we only need one half of the avocado, use the half that does not have a seed, and put the other one in the refrigerator. Pull the avocado out of the skin using a spoon. Put the avocado and cherry tomatoes on a plate, next to the roll.
To bring out the flavor of the avocado, add a pinch of salt and freshly ground pepper.

The edited Royal Breakfast containing more vegetables

Check the status after you saved the recipe

Once you finished editing the recipe, and you saved the file, Git will notice that the file has been changed.

To check if Git has something interesting to say, you can use the command you already know, that is git status. This time, Git is telling you that royal_breakfast.txt is modified, which means Git already tracks this file, and knows what’s going on with the recipe.

Git is showing the recipe is modified

What has been changed in the recipe?

A really useful command that Git offers is git diff, which shows you what exactly has been changed in a text file.

The git diff command can be used in a few scenarios, for example:

  • If you modified your file (e.g. edited the recipe in the notepad), but you haven’t staged these changes yet (so you didn’t use the git add command), use git diff without parameters, and this will show you the difference between the working directory and the last commit
  • If you modified the recipe, and you have already staged the changes using git add, use git diff --cached, which will show you the difference between changes you staged and the last commit
You can verify the exact changes using the ‘git diff’ command

Add changes to the staging area

I’m satisfied with modifications introduced in the recipe, so it’s time to stage and commit these changes.

The green highlighted file means that the changes it contains will be committed

What will be committed?

Now, I already staged the change I want to commit, so git status is highlighting the royal_breakfast.txt file with green color. But what if you want to double-check what differences will be committed?

Let’s enter git diff once again. As you can see, the output is now empty, despite changes are waiting in the index/staging area for the next commit!

If I enter git diff --cached, however, the diff output is correct.

This particular situation is just a specific behavior of the git diff command that you have to keep in mind, otherwise you’ll be surprised what’s going on.

The ‘git diff’ command shows no differences after moving changes to the index
Using ‘cached’ flag for ‘git diff’ command shows the differences after changes were already staged

Save the new snapshot containing more vegetables

Our changes are moved to the staging area, so they are ready to commit. Enter git commit -m "Add more vegetables", and that’s it — the repository has another snapshot of our breakfast.

Another changes in the recipe committed

Let’s add some more goodies!

My favorite part of breakfast, is frying scrambled eggs for me and my wife. Okay, I lied to you — more than cooking the scrambled eggs, I love eating them even more!

So let’s add the final part of the Royal Breakfast, the icing on the cake:

Ingredients:
- 1 roll
- cream cheese
- sprouts
- 1/2 of avocado
- 3–4 cherry tomatoes
- 1/2 of onion
- a few slices of ham
- 2 eggs
- 1 tbsp of olive oil

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

Slice an avocado around the pit and halve it. Because we only need one half of the avocado, use the half that does not have a seed, and put the other one in the refrigerator. Pull the avocado out of the skin using a spoon. Put the avocado and cherry tomatoes on a plate, next to the roll.
To bring out the flavor of the avocado, add a pinch of salt and freshly ground pepper.

Finely chop the onion. In a frying pan, heat the olive oil and fry the onion for one minute over medium heat. Add the ham and cook another minute. If, like me, you’re not a fan of mousse-like scrambled eggs, don’t whisk them before you add them to the pan — just gently mix the eggs directly on the pan. Add a pinch of salt. Control the fire and gently stir the eggs from time to time to prevent them from sticking to the pan. Finally, add freshly ground black pepper.

Enjoy!

As you may have already guessed, you should now add the changes to the staging area (git add), and then commit them (git commit).

More changes waiting to be added to the staging area
Changes have been added to the staging area
Git is showing that the changes are related to scrambled eggs
Finally, the Royal Breakfast includes scrambled eggs too

The Royal Breakfast in all its glory

At last, I can enjoy my favorite breakfast. But, didn’t I write at the beginning that I’ll explain commits and how to read the history of the repository?

Oh yeah, I’ll do it but in the next episode, because it turned out that this article has become as big as the Royal Breakfast.

The Royal Breakfast in all its glory

References

[1] Krystian Szpiczakowski, How to start cooking with Git (Meet Git #4) https://medium.com/codex/how-to-start-cooking-with-git-meet-git-4-a286e4e08109

--

--

Krystian Szpiczakowski
CodeX

The dev who enjoys writing tests / Home-grown barista