Tracking changes

Jose Robledo

04 March 2025

Objectives

  • Go through the modify-add-commit cycle for one or more files.
  • Explain where information is stored at each stage of that cycle.
  • Distinguish between descriptive and non-descriptive commit messages.

Questions

  • How do I record changes in Git?
  • How do I check the status of my version control repository?
  • How do I record notes about what changes I made and why?

Go to recipes

$ cd ~/Desktop/recipes

create a file called guacamole.md that contains the basic structure of our first recipe.

$ nano guacamole.md

Include content

# Guacamole
## Ingredients
## Instructions

Save it, exit your editor, and check it was succesfully created

$ ls

View content of file with cat

$ cat guacamole.md

check status of our repository

$ git status

We can tell Git to track a file using git add

$ git add guacamole.md

check the status again. What changed?

Commiting

Git now knows that it’s supposed to keep track of guacamole.md, but it hasn’t recorded these changes as a commit yet. To get it to do that, we need to run one more command:

$ git commit -m "Create initial structure for a Guacamole recipe"

git commit, Git takes everything we have told it to save by using git add and stores a copy permanently inside the special .git directory. This permanent copy is called a commit (or revision) and has an identifier and a short identifier.

On commit message

We use the -m flag (for “message”) to record a short, descriptive, and specific comment that will help us remember later on what we did and why.

Good commit messages start with a brief (<50 characters) statement about the changes made in the commit. Generally, the message should complete the sentence “If applied, this commit will” . If you want to go into more detail, add a blank line between the summary line and your additional notes. Use this additional space to explain why you made changes and/or what their impact will be.

Commit history

If we want to know what we’ve done recently, we can ask Git to show us the project’s history using git log:

$ git log

Alfredo now wants to add more information to the Guacamole recipe, let’s say he wants it to look like:

# Guacamole
## Ingredients
* avocado
* lemon
* salt
## Instructions
  • modify the file to include the changes
  • run git status and observe the output

Reviewing changes

We have changed this file, but we haven’t told Git we will want to save those changes (which we do with git add) nor have we saved them (which we do with git commit).

$ git diff

comment: you might need to press q to exit the git diff commmand. If you have colors on your terminal, green (and + symbols) are addition and red (and - symbols) are deletions.

  • exercise: add, review, and commit the changes with commit message “Add ingredients for basic guacamole”.

staging

Git has a special staging area where it keeps track of things that have been added to the current changeset but not yet committed.

  • git add specifies what will go in the staging area,
  • git commit then actually saves the current snapshot of the repository, by making a permanent record of the changes (as a commit).

A diagram showing how "git add" registers changes in the staging area, while "git commit" moves changes from the staging area to the repository

exercise

  • Improve our recipe by changing ‘lemon’ to ‘lime’ in guacamole.md

  • check the difference with git diff.

  • Since we agree with the changes, let’s add them to the staging area, and now see which are the differences again with git diff. Anything different?

Solution:

$ git diff --staged

If you’re satisfied, save your changes (commit)!

$ git commit -m "Modify guacamole to the traditional recipe"

  • comment: Sometimes, e.g. in the case of the text documents a line-wise diff is too coarse. That is where the --color-words option of git diff comes in very useful as it highlights the changed words using colors.

  • When the output of git log is too long to fit in your screen, git uses a program to split it into pages of the size of your screen. When this “pager” is called, you will notice that the last line in your screen is a :, instead of your usual prompt.

    • To get out of the pager, press Q.
    • To move to the next page, press Spacebar. To go back a page, press B.
    • To search for some_word in all pages, press / and type some_word. Navigate through matches pressing N. To navigate back matches press Shift+N.

Limiting Log size

  • git log -N: Last N commits. For example
$ git log -1
  • --online: reduce quantity of information
$ git log --oneline
  • --graph: display commit history as text-based graph
$ git log --graph

Directories

  • Git does not track directories on their own, only files within them. To force Git to keep track of an empty directory, some people add an empty hidden file .gitkeep inside the directory.
$ mkdir cakes
$ git status
$ git add cakes
$ git status
  • If you create a directory in your Git repository and populate it with files, you can add all the files in the directory at once by referring to the directory in your git add command.
$ touch cakes/brownie_cakes/lemon_drizzle
$ git status
$ git add cakes
$ git status
  • Commit these changes with message ““Add some initial cakes”” so we are in the same state

Choosing a Commit Message

Which of the following commit messages would be most appropriate for the last commit made to guacamole.md?

  1. “Changes”
  2. “Change lemon for lime”
  3. “Guacamole modified to the traditional recipe”

exercise

Committing Multiple Files

  • The staging area can hold changes from any number of files that you want to commit as a single snapshot.

    1. Add some text to guacamole.md noting the rough price of the ingredients.
    2. Create a new file groceries.md with a list of products and their prices for different markets.
    3. Add changes from both files to the staging area, and commit those changes.

exercise

bio Repository

  • Create a new Git repository on your computer called bio.
  • Write a three-line biography for yourself in a file called me.txt, commit your changes
  • Modify one line, add a fourth line
  • Display the differences between its updated state and its original state.

Continue with

05. Exploring History

Content

01. Intro to Git

02. Setting up Git

03. Creating a repository

04. Tracking changes

05. Exploring History

06. Ignoring things

07. Remotes in GitHub

08. Collaborating

09. Branching and Merging