Creating a Repository and Tracking Changes

Overview

Teaching: 10 min
Exercises: 0 min
Questions
  • How do I get Git to track my files?

  • How do I record changes in Git?

  • How do I make notes about my changes?

Objectives
  • Create a local Git repository.

  • Go through a modify-add-commit cycle for one or more files.

  • Distinguish between descriptive and non-descriptive commit messages.

Configuring git

If this is your first time running git on this computer, then when you launch GitHub Desktop it will ask you questions about your email address, user name, etc.

Create a New Repository

Once Git is configured, we can start using it.

In GitHub Desktop, go to File >> New Repository, or type control-N (command-N on a mac). Name your repository planets and put it in the directory you’re using for this workshop. Leave “Initialize this repository with a README” unchecked and leave Git ignore and License as “None”.

Create a New Repository

 

You will see that a new directory called planets has been created on your computer in the location you specified.

Warning! If there is already a directory with the name you chose in the specified location, it will appear unchanged, but your computer will invisibly install some git machinery inside the directory.

Places to Create Git Repositories

Dracula starts a new project, moons, related to his planets project. Despite Wolfman’s concerns, he creates one Git repository inside another in GitHub Desktop. Why is this not a good idea?

Solution

Git repositories can interfere with each other if they are “nested” in the directory of another: the outer repository will try to version-control the inner repository. Therefore, it’s best to create each new Git repository in a separate directory. Note that we can track files in directories within a Git repository.

Tracking Changes

Open a plain text editor (TextEdit on a Mac, Notepad in Windows). This shouldn’t be a word processor like Microsoft Word or LibreOffice Write. Start a new file and type in the following text:

Cold and dry, but everything is my favourite colour

Make sure the format is plain text and save this file with the name mars.txt inside the planets directory.

Plain Text in TextEdit

 

Notice that the file has now appeared in the left sidebar in GitHub Desktop. If you click on it, you’ll see the line you just typed as well. The line is highlighted in green and begins with a +. That means it has been added. Git is now paying attention to the new file mars.txt and has started adding it to the staging area, but has not yet committed (taken a snapshot) of the file.

Line Has Been Added

 

Depending on your operating system, some other files might have appeared in the sidebar as well. Uncheck the boxes next to them to remove them from the staging area. Later we’ll learn how to ignore them automatically.

To commit mars.txt, you first need to enter a commit message in the text box labeled “Summary” below the list of files. Our commit message is

Start notes on Mars as a base

Good commit messages are a brief (<50 characters) summary of changes made in the commit. If you want to go into more detail, add that information in the “Description” box.

Now that you have entered your commit message, you can click on the button labeled “Commit to master.”

Ready To Commit

 

Oh no! It looks like the files and changes disappeared! Don’t panic. If you go back to your file browser and text editor, you’ll see that they’re still there. GitHub Desktop isn’t tracking the existence of files in your repository, it’s tracking the changes. There haven’t been any changes since your last commit, so GitHub Desktop has nothing to show you in the “Changes” tab.

No Changes

 

To see a list of the commits we have already made, we can switch to the History tab in the left sidebar.

History

 

Add a second line to the file mars.txt and save it. Watch what happens in GitHub Desktop before and after you save mars.txt

Cold and dry, but everything is my favorite color
The two moons may be a problem for Wolfman

Commit them with the message

Add concerns about the effects of Mars' moons on Wolfman

What happens if we’ve made multiple changes in the file, but we want to take separate snapshots of each one?

Staging Area

If you think of Git as taking snapshots of changes over the life of a project, the blue bars next to the changed lines specify what will go in a snapshot (putting things in the staging area), and pressing “commit to master” then actually takes the snapshot, and makes a permanent record of it (as a commit). It’s a good idea to pay attention to what’s being staged, and try to commit things that fit together conceptually.

Directories

An important fact you should know about directories in Git:

Git does not track directories on their own, only files within them. Try it for yourself:

  • Create a new directory in your planets repo

Note that your newly created empty directory does not appear in the list of changes. This is the reason why you will sometimes see .gitkeep files in otherwise empty directories. Unlike .gitignore, these files are not special and their sole purpose is to populate a directory so that Git adds it to the repository. In fact, you can name such files anything you like. As soon as you put a file in the new directory, that file, inside that directory, will appear in your Changes sidebar. Try it!

Choosing a Commit Message

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

  1. “Changes”
  2. “Added line ‘But the Mummy will appreciate the lack of humidity’ to mars.txt”
  3. “Discuss effects of Mars’ climate on the Mummy”

Solution

Answer 1 is not descriptive enough, and answer 2 is too descriptive and redundant, but answer 3 is good: short but descriptive.

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 mars.txt noting your decision to consider Venus as a base
  2. Create a new file venus.txt with your initial thoughts about Venus as a base for you and your friends
  3. All your changes now appear in GitHub Desktop. Decide whether they go together in one commit, or need separate commits, and use the blue bars and ticky boxes and unstage or restage the lines. When you’re satisfied, commit.

Key Points

  • Initialize a new repository with File >> New Repository.

  • Changes saved in files get staged in Git.

  • Once you’ve made and reviewed your changes, committing them takes a snapshot of your repository.