Introduction to Version Control using Git#



  • How do I use git to keep a record of my project?


  • Explain the purpose of version control.

  • Introduce common git commands.

  • Understand how to create a commit.

  • Understand how to view diffs and see previous versions of files.

Follow Along with This Lesson

To follow along with this lesson, you can complete the previous lessons, or you can download a pre-made workshop repository that is at the starting point.

You will need to make sure that you have git installed and configured, as described in the set-up instructions.

git clone
cd molecool
git checkout git-start
git switch -c main

You can also download the pre-made workshop repository as a zip file. If downloading as a zip file, you will need to initialize git in the repository and make an initial commit in order to use git.

What is version control?#

Version control keeps a complete history of your work on a given project. It facilitates collaboration on projects where everyone can work freely on a part of the project without overwriting others’ changes. You can move between past versions and roll back when needed. Also, you can review the history of your project through commit messages that describe changes in the source code, and see what exactly has been modified in any given commit. You can see who made the changes and when it happened.

This is greatly beneficial whether you are working independently or within a team.

git vs. GitHub

git is the software used for version control, while GitHub is a hosting service. You can use git locally (without using an online hosting service), or you can use it with other hosting services such as GitLab or BitBucket.

Other examples of version control software include Subversion (svn) and Mercurial (hg).

Making Commits#

You should have git installed and configured from the setup instructions.

In this section, we are going to edit files in the Python package that we created earlier and use git to track those changes.

First, use a terminal to cd into the top directory of the local repository (the outer molecool directory).

In order for git to keep track of your project, or any changes in your project, you must first tell it that you want it to do this. You must manually create checkpoints in your project if you wish to have points to return to. If you were not using the CookieCutter, you would first have to initialize your project (i.e. tell git that you were working on a project) using the command git init.

When we ran the CMS CookieCutter, it actually initialized git for us, added our files, and made a commit (how convenient!). We can see this by typing the following into the terminal on Linux or Mac

ls -la

Here, the -la says that we want to list the files in long format (-l), and show hidden files (-a).

You should see .git in the output. .git is a directory where git stores the repository data. This is one way to see that we are in a git repository.

Next, type

git status
On branch main
nothing to commit, working tree clean

This tells us that we are on the main branch (more about branching later) and that no files have been changed since the last commit.

Next, type

git log

You will get an output resembling the following. This is something called your git commit log. Whenever you make a version, or checkpoint, of your project, you will be able to see information about that checkpoint using the git log command. The CookieCutter has already made a commit and written a message for you, and that is what we look for in this first commit in the log.

commit 25ab1f1a066f68e433a17454c66531e5a86c112d (HEAD -> main, tag: 0.0.0)
Author: Your Name <>
Date:   Mon Feb 4 10:45:26 2019 -0500

    Initial commit after CMS Cookiecutter creation, version X.X

Your version number for the Cookiecutter will depend on when you ran the Cookiecutter and the current released version.

Each line of this log tells you something important about the commit, or checkpoint, that exists for the project. In the first line,

commit 25ab1f1a066f68e433a17454c66531e5a86c112d (HEAD -> main, tag: 0.0.0)

You have a unique identifier for the commit (25ab1…). You can use this hexadecimal number to reference this checkpoint.

Then, git records the name of the author who made the change.

Author: Your Name <>

This should be your information. This way, anyone who downloads this project can see who made each commit. Note that this name and email address match what you specified when you configured git in the setup, with the name and email address you specified to cookiecutter having no effect.

Date:   Mon Feb 4 10:45:26 2019 -0500

Next, it lists the date and time the commit was made.

    Initial commit after CMS Cookiecutter creation, version 1.0

Finally, there will be a blank line followed by a commit message. The commit message is a message that whoever made the commit chose to write but should describe the change that took place when the commit was made. This commit message was written by cookiecutter for you.

When we have more commits (or versions) of our code, git log will show a history of these commits, and they will all have the same format discussed above. Right now, we have only one commit: the one created by the CMS CookieCutter.

The 3 steps of a commit#

Now, we will change some files and use git to track those changes.

Let’s edit our README file. Open in your text editor of choice. On line 8, you should see the description of the repository we typed when running cookiecutter.

Add the following sentence to your under the initial description and save the file.

This repository is currently under development. To do installation in development mode, download this repository and type

`pip install -e .`

in the repository directory.

git add, git status, git commit#

Making a commit is like making a checkpoint for a particular version of your code. You can easily return to, or revert to that checkpoint.

To create the checkpoint, we first have to make changes to our project. We might modify many files at a time in a repository. Thus, the first step in creating a checkpoint (or commit) is to tell git which files we want to include in the checkpoint. We do this with a command called git add. This adds files to what is called the staging area.

Let’s look at our output from git status again.

On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)


no changes added to commit (use "git add" and/or "git commit -a")

Git even tells us to use git add to include what will be committed. Let’s follow the instructions and tell git that we want to create a checkpoint with the current version of

git add
git status
On branch main
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)


We are now on the second step of creating a commit. We have added our files to the staging area. In our case, we only have one file in the staging area, but we could add more if needed.

To create the checkpoint, or commit, we will now use the git commit command. We add a -m after the command for “message”. Whenever you create a commit, you should write a message about what the commit does.

git commit -m "update readme to have instructions for developmental install"

Now when we look at our log using git log, we see the commit we just made along with information about the author and the date of the commit.

If you neglect the -m option and configure an editor during set-up, git will open the editor for you to compose your commit message.

Let’s continue to edit this readme to include more information. This is a file which will describe what is in this directory. Open in your text editor of choice and add the following to the end.

This package requires the following:
  - NumPy
  - matplotlib

This file is using a language called Markdown.

Check Your Understanding

Create a commit for the changes we’ve made to your repository.

Once you have saved this file, type

git log

Now, check git status and git log. You should see the following:

git status
On branch main
nothing to commit, working tree clean
git log

We now have a log with three commits. This means there are three versions of the repository we are working in.

git log lists all commits made to a repository in reverse chronological order. The listing for each commit includes the commit’s full identifier, the commit’s author, when the commit was created, and the commit title.

We can see differences in files between commits using git diff.

git diff HEAD~1

The argument to git diff refers to the comparison point in our commit history. HEAD is an alias for the commit at the tip of our checked-out branch. ~1 is a modifier that refers to the given commit minus 1. We are asking git to show us the difference between the current files and the second most recent commit.

Lines that have been added are indicated in green with a plus sign next to them (+), while lines that have been deleted are indicated in red with a minus sign next to them (-).

Viewing previous versions#

If you need to check out a previous version,

git checkout COMMIT_ID

This will temporarily revert the repository to whatever the state was at the specified commit ID.

Let’s check out the version before the most recent edit we made to the README.

git log --oneline
d857c74 (HEAD -> main) add information about dependencies to readme
3c0e1c6 update readme to have instructions for developmental install
116f0cf (tag: 0.0.0) Initial commit after CMS Cookiecutter creation, version 1.1

In this log, the commit ID is the first number on the left.

To revert to the version of the repository where we first edited the readme, use the git checkout command with the appropriate commit ID.

git checkout 3c0e1c6

If you now view your, it has reverted to the previous version of the file.

To return to the most recent point,

git checkout main

Exercise - Creating a Repository#


What list of commands would mimic what the CMS CookieCutter did when it created the repository and made the first commit? (Hint - to initialize a repository, you use the command git init.)

Exploring git history#

When working on a project, it is easy to forget exactly what changes we have made to a file. To check this, do

git diff HEAD

We should get a blank result. “HEAD” is referencing the most recent commit. Since we committed our changes to, there is no difference to show.

Open your and add the following line to the end of it.

This line doesn't add any value

Save that file and run the same command.

git diff HEAD
diff --git a/ b/
index 94e0b50..a68f349 100644
--- a/
+++ b/
@@ -17,6 +17,8 @@ This package requires the following:
   - numpy
   - matplotlib

+This line doesn't add any value.
 ### Copyright

To compare against the commit just before the most recent commit, add “~1” to the end of “HEAD”:

git diff HEAD~1
diff --git a/ b/
index e778cd4..94e0b50 100644
--- a/
+++ b/
@@ -13,6 +13,10 @@ This repository is currently under development. To do installation in development mode, download this repository and type

`pip install -e .`

in the repository directory.

+This package requires the following:
+  - numpy
+  - matplotlib
 ### Copyright

If we want to compare against a specific commit, we can first do git log to find the commit’s ID, and then do:

git diff *commit_id*

Another problem that we sometimes encounter is wanting to undo all of our changes to a particular file. This can be done with

git checkout HEAD

If you open you will see that it has reverted to the content from the most recent commit.

Of course, you could also replace HEAD here with HEAD~1 or a specific commit ID.

Creating new features - using branches#

When you are working on a project to implement new features, it is a good practice to isolate the changes you are making and work on one particular topic at a time. To do this, you can use something called a branch in git. Working on branches allows you to isolate particular changes. If you make sure that your code works before merging to your main or master branch, you will ensure that you always have a working version of code on your main branch.

If you followed the set-up instructions, you should be in the main branch by default. To create a new branch and move to it, you can use the command

git checkout -b new_branch_name

The command git checkout switches branches when followed by a branch name. When you use the -b option, git will create the branch and switch to it. For this exercise, we will add a new feature: We are going to add a function to print the Zen of Python.

First, we’ll create a new branch:

git checkout -b zen

We can visualize what branching looks like with some simple figures. Before branching, imagine a git commit history that looks like this. In the diagram below, each circle represents a git commit. There have been two commits, and the HEAD is currently after commit 2.


After we have created a new branch and checked it out, we can imagine our git history looking like this. The zen branch ‘branches’ starts from the point where we used the git branch command.


Now, when we make a commit on the zen branch, our changes will continue from this point, leaving the main branch unchanged. Note that we have not yet made a commit, but this diagram is for illustrative purposes.


Now that we have a better understanding of what branching looks like, let’s make some changes to the zen branch. Add a new function to your module. We are going to add the ability to print “The Zen of Python”. You can get the Zen of Python by typing

import this

into the interactive Python prompt.

This will print the Zen of Python to the screen. The Zen of Python is a collection of 19 software principles that influence the design of Python.

Let’s make a function (similar to our existing canvas function) that prints the Zen of Python.

Add the following function to your module.

def zen(with_attribution=True):
    quote = """Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!"""

    if with_attribution:
      quote += "\n\tTim Peters"

    return quote

Verify that this function works in the interactive Python prompt. Next, commit this change:

git add .
git commit -m "add function to print Zen of Python"

Let’s switch back to the main branch to see what it is like. You can see a list of branches in your repo by using the command

git branch

This will list your local branches. The active branch, or the branch you are on will be noted with an asterisk (*).

To switch back to the main branch,

git checkout main

When you look at the module on the main branch, you should not see your most recent changes.

You can verify this by using the git log command.

Consider that at the same time we have some changes or features we’d like to implement. Let’s make a branch to do a documentation update.

Create a new branch.

git checkout -b doc_update

Let’s add some information about developing branches to the README. Update your README to include this information:

Features should be developed on branches. 
To create and switch to a branch, use the command

`git checkout -b new_branch_name`

To switch to an existing branch, use

`git checkout new_branch_name`

Save and commit this change.

git add
git commit -m "add information about branching to readme"

To incorporate these changes in main, you will need to do a git merge. When you do a merge, you should be on the branch you would like to merge into. In this case, we will first merge the changes from our doc_update branch, then our zen branch, so we should be on our main branch. Next, we will use the git merge command.

The syntax for this command is git merge branch_name where branch_name is the name of the branch you would like to merge.

We can merge our doc_update branch to get changes on our main branch:

git checkout main
git merge doc_update

Now our changes from the branch are on main.

We can merge our zen branch to get our changes on main:

git merge zen

This time, you will see a different message and a text editor will open for a merge commit message.

Note that if you have an older version of git, you may see Merge made by recursive strategy. instead instead of Merge made by the 'ort' strategy.

Merge made by the 'ort' strategy.

This is because main and zen have development histories that have diverged. git had to do some work in this case to merge the branches. A merge commit was created.

Merge commits create a branched git history. We can visualize the history of our project by adding --graph. There are other workflows you can use to make the commit history more linear, but we will not discuss them in this course.

Once we are done with a feature branch, we can delete it:

git branch -d zen

Exercise - Using Branches#

Using Branches - Exercise

For this exercise, you will be adding all the functions from your Jupyter Notebook to the package. Create a branch to add your functions. Add all of the functions from your Jupyter Notebook to the module in your package. Verify that you can use your functions. Once the functions are added and working, merge them into your main branch.

Adding data#

We now have a package with some functions. Let’s add the data from our starting material to our package as well. We will add this to the molecool/testing/data directory. Although it would be a best practice to add these files through a branch, we will add them directly to the main branch for simplicity.

Assuming that you ran the cookiecutter from the starting material directory,

cp -r ../data molecool/tests/

Then, commit the change:

git add .
git commit -m "add data to package"

Ignoring files - .gitignore#

Sometimes while you work on a project, you may end up creating some temporary files. For example, if your text editor is Emacs, you may end up with lots of files called <filename>~. By default, Git tracks all files, including these. This tends to be annoying since it means that any time you do git status, all of these unimportant files show up.

We are now going to find out how to tell Git to ignore these files so that it doesn’t keep telling us about them every time we do git status. Even if you aren’t working with Emacs, someone else working on your project might. So let’s do the courtesy of telling Git not to track these temporary files. First, let’s ensure that we have a few dummy files. Make empty files called testing.txt~ and in your repository using your text editor of choice.

While we’re at it, also make some other files that aren’t important to the project. Make a file called calculation.out in molecool/data using your text editor of choice.

Now check what Git says about these files:

git status
On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

nothing added to commit but untracked files present (use "git add" to track)

Now we will make Git stop telling us about these files.

Earlier, when we looked at the hidden files, you may have noticed a file called .gitignore. Cookiecutter created this for us, however, GitHub also has built-in .gitignore files you can add when creating an empty repository.

This file is to tell git which types of files we would like to ignore (thus the name .gitignore).

Look at the contents of .gitignore

# Byte-compiled / optimized / DLL files

# C extensions

# Distribution / packaging

# PyInstaller
#  Usually these files are written by a Python script from a template
#  before PyInstaller builds the exe, so as to inject date/other infos into it.


Git looks at .gitignore and ignores any files or directories that match one of the lines. Add the following to the end of .gitignore:

# emacs

# temporary data files

Now do “git status” again. Notice that the files we added are no longer recognized by Git.

git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   .gitignore

no changes added to commit (use "git add" and/or "git commit -a")

We want these additions to .gitignore to become a permanent part of the repository:

git add .gitignore
git commit -m "Ignores Emacs temporary files and input/output files from calculations."

One nice feature of .gitignore is that prevents us from accidentally adding a file that shouldn’t be part of the repository. For example:

git add data/
The following paths are ignored by one of your .gitignore files:
Use -f if you really want to add them.

It is possible to override this with the -f option for git add.

Final Repository State

You can see the final state of the repository after this section here.

You can also download a zip of the repository here.

More Tutorials#

If you want more git, see the following tutorials.

Basic git#

Final Repository State

You can see the final state of the repository after this section here.

You can also download a zip of the repository here.

Key Points#

Key Points

  • Git provides a way to track changes in your project.

  • Git is a software for version control and is separate from GitHub.