Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Learning Objectives:

  • Describe the concept of version control and why it is important to use

  • Utilize the diff and patch commands to automate differentiating and editing files

  • Explain what Git is and its benefits of use

  • Install Git on local machine

  • Utilize Git to create and clone repositories, add code, check the status of code, and commit code

Table of Contents:

Table of Contents
minLevel1
maxLevel7

Course Introduction

This course focuses on how to keep track of the different versions of your code and configuration files using version control systems or VCS.

In this course, we'll introduce you to a popular VCS called Git, and show you some of the ways you can use it.

We'll also go through how to set up an account with the service called GitHub, so that you can create your very own remote repositories to store your code and configuration.

...

Diffing Files

We can use the diff command line tool to take two files or even to directories, and show the differences between them in a few formats.

Example:

We have two files rearrange 1.py and rearrange 2.py which contain two different versions of the same function.

When we call the diff command: diff rearrange1.py rearrange2.py

...

We get only the lines that are different between two files.

See the symbols at the beginning of each of those lines? The “<“ symbol tells us that the first line was removed from the first file, and the “>” symbol tells us that the second line was added to the second file. In other words, the old line got replaced by the new one.

Example:

...

Here there are more changes going on. We can see that diff splits the changes in two separate sections.

...

The section that starts with 11a13,15 shows three lines that are new in the second file. The a stands for added, but that block looks a bit strange doesn't it? It seems like we're adding a return and an if condition but nobody for the if block. What's up with that? To understand this better we can use the -u flag to tell diff to show the differences in another format.

...

There are a lot of tools out there to compare files. Diff is the most popular one, but not the only one available. For example, wdiff highlights the words that have changed in a file instead of working line by line like diff does.

...

To do this, we typically use a command line like: diff -u old_file new_ file > change.diff

As a reminder, the greater than sign redirects the output of the diff command to a file. So with this command, we're generating a file called change.diff with the contents of diff -u command.

The generated file is usually referred to as a diff file or sometimes a patch file. It includes all the changes between the old file and the new one, plus the additional context needed to understand the changes and to apply those changes back to the original file.

There's a command called patch that takes a file generated by diff and applies the changes to the original file. How do we do that? We'll pass the name of the file that we want to patch as the first parameter to the command and then we'll provide the diff file through standard input.

...

  1. The first step is to check whether you already have it installed. You can do this by running git --version. If you're running a version number higher than 2.20, then you can just use that one. If you get an error message or an older version number, you'll need to install the current version.

  2. If you use a package management system like apt or yum on Linux, Chocolatey on Windows, or Homebrew on Mac OS, you can just install Git through that. If you don't use a package management system, then you can download the latest executable installer from the official website and deploy it on your computer.

    1. On Linux, installing and using Git is pretty straightforward. You can install it with the command apt

      install git or yum install git, and after that, you'll have Git installed and ready to use.

    2. On Mac OS, you can even have it installed when you run git --version. If Git isn't installed, this command will ask you if you want to install it and then download it and install it for you. Alternatively, you can also download it from the website and install it by following the prompts. Once it's installed, you'll be able to use it from the command line just like any other tool.

    3. On Windows, after downloading and executing the installer, you'll need to go through a bunch of different configuration options. These options come with preselected defaults that usually makes sense to just keep. Pay attention to the editor question though. You'll probably want to change

      the editor to one that you feel comfortable with, like Notepad++ or Atom. One interesting thing about

      the Windows installation is that it comes preloaded with an environment called MinGW64. This environment lets us operate on Windows with the same commands and tools available on Linux. So you can practice some Linux command line tools on your Windows machine. After installing Git on your Windows machine, you'll be able to use Git from the Linux command line. If you selected the default option for the path environment question, you'll be able to also run it from the PowerShell command line (An optional video will talk about the available options and when you might want to select something different from the default).

...

Remember when we said that a VCS tracks who made which changes, for this to work, we need to tell Git who we are. We can do this by using the Git config command and then setting the values of user.email and user.name to our email and our name like this.

...

We can create one from scratch using the git init command or we can use the git clone command to make a copy of a repository that already exists somewhere else. We'll talk about remote repositories later in the course.

...

We can check that this directory exist using the ls -la command which lists files that start with a dot.

We can also use the ls -l .git/ command to look inside of it and see the many different things it contains.

...

To make Git track our file, we'll add it to the project using the git add command passing the file that we want as a parameter. With that, we've added our file to the staging area.

The staging area (index) is a file maintained by Git that contains all of the information about what files and changes are going to go into your next command.

We can use the git status command to get some information about the current working tree and pending changes. We see that our new file is marked to be committed, this means that our change is currently in the staging area. To get it committed into the.git directory, we run the git commit command.

When we run this command, we tell Git that we want to save our changes. It opens a text editor where we can enter a commit message. If you want, you can change the editor used to your preferred editor. In our case, this computer has nano configured as a default editor.

...

  1. Will first be modified when we change it in any way.

  2. Then it becomes staged when we mark those changes for tracking.

  3. And finally it will get committed when we store those changes in the VCS.

...

Example Git repo:

First, let's check the contents of the current working tree using ls -l. And then the current status of our files using the Git status command.

When we run Git status, Git tells us a bunch of things, including that we're on the master branch.For now, notice how it says that there's nothing to commit and that the working tree is clean. Let's modify a file to change that. For example, we'll just add periods at the end of the message that our script presents to the user.

...

Let's change that by running the Git add command, passing the disk_usage.py file as a parameter. When we call Git add, we're telling Git that we want to add the current changes in that file to the list of changes to be committed. This means that our file is currently part of the staging area, and it will be committed once we run the next Git command, Git commit.

In this case, instead of opening up an editor, let's pass the commit message using the - m flag, stating that we added periods at the end of the sentences.

...

We initialize a new repository by running the git init command in any file system directory.

For example, let's use the mkdir command to create a directory called scripts, and then change into it and initialize an empty Git repository init.

Our shiny new Git repository can now be used to track changes to files inside of it. But before jumping into that, let's check out our current configuration by using the git config -l command.

For now, pay special attention to the user.email and the user.name lines, which we touched on briefly in an earlier video.

...

To stage our changes, we need to call git add once again. We have to call git commit to store those changes to the Git directory. This time, we'll use the other way of setting the commit message.

We'll call git commit -m, and then pass the commit message that we want to use. So in this case, we'll say that we've added the check_reboot function.

...

There's a git command used to display these commit messages called git log. This command will do any line wrapping for us. Which means that if we don't stick to the recommended line wrapping, long commit messages will run off the edge of the screen and be difficult to read.

...