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

...

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.

...