Introduction

Author

Government Analysis Function and ONS Data Science Campus

Git is a version-control system used to track changes in source code during software development. It was created in 2005 to help with the development of Linux, and is an incredibly common way of managing your code.

It allows multiple developers to work with the same code simultaneously, while tracking the history of every line of code written. It also allows users to create branches, where code can be edited and experimented with, while leaving the original code untouched, removing the chance of overwriting essential code. The image below demonstrates a common practice when not using version control software such as Git:


A windows directory structure with a set of badly named test script files


Git can also be used to backup your work onto remote servers to assist with collaboration and mitigate data loss in the event of a system failure. This brings us to an important distinction:

Please ensure before continuing the course you have completed all the pre-course requirements, including requesting Github (or your department’s equivalent) and requesting access to a project.

1 Git Bash

Git Bash is a program used to interact with Git via command line, though when it is installed, a Git GUI (Graphical User Interface) is included. Although typically used on Windows machines, Git Bash uses the Linux dialect of command line command. For example, to list the contents of a directory:

  • Linux (and Git Bash) uses ls
  • Windows uses dir

If Git is installed, a Git Bash window can be opened to a specific directory by opening the folder, right clicking, and selecting Git Bash Here.

Git Bash can be also opened in the Start menu, but you will need to navigate within Git Bash using command line commands to the location of each project.

If you do not have Git Bash, other command line interfaces can be used in a very similar fashion. For ONS devices, we will just look at Git Bash. Use the interface suggested in your department.


Shows the Git Bash Here option from the right click option menu in Windows Explorer


1.1 Syntax

Git Bash may seem intimidating at first if you are unfamiliar with command line tools. We use the command line to more directly work with our computer, increasing our ability to specify what we want and develop code at pace.

Github comes with a user interface which allows some small changes to be made, this should be done sparingly as it is not as clear to others what you have done, and does not scale well with large projects.

When we open the Git Bash window we will see a line with a $, this is where we type our commands. We can type one command line at a time here.

To run the command you can press Enter.

Each part of a command is separated by a space, with each argument having some different effect on the command.

When doing Git based things all our commands start with git in the form:

$ git <other commands>

Note, the <> are not included in real commands and are just placeholders.

An example command would therefore be:

$ git add example_file.txt

Some other useful general command line commands for within Git Bash are:

$ cd folder/subfolder   # Change Directory (cd) to a folder within the current directory
$ cd ..                 # Change Directory to a level one higher 
$ ls                    # List all folders and files within the current directory

2 Global Config Settings

When starting out using Git, there are a few key things that need to be set. The first one, is to tell Git who we are. This allows it to record which developer wrote which pieces of code, and allow you to work collaboratively.

$ git config --global user.name "Scooby Doo"
$ git config --global user.email scooby.doo@mysterymachine.co.uk

If these aren’t set, it will have a guess at your details with an unknown amount of success - on a networked, government machine it will probably be able to get your email address, and a relevent username.

3 Creating a Repository

Once your config settings have been set, a new repository can be created on your computer. There are different ways of doing this, from creating a new, empty repository, to cloning one that can be found on the internet.

The simplest way, is to initialise a new local repository using the following command:

$ git init
Initialized empty Git repository in //<directory>/<directory>/<directory>/<directory>/.git/

This creates a hidden folder within your directory called .git, which stores the history of the repository. If that folder gets deleted, history is erased!

When working with real projects you will want to create a repository on your machine where you keep all your projects. We will refer to where your .git file is as the repository. After completing this course, create a new “Git Area” folder somewhere appropriate on your machine. If you work at ONS, this could be on your D: drive. If you work elsewhere, make sure to store your folder somewhere you have edit rights. This will be the parent folder of all your repositories. Each repository will have it’s own folder within, and it’s own .git file.

You can make an empty git repository within a specific directory by writing:

$ git init <directory>

4 Exercises

  1. Create a folder on your desktop called ‘Intro_to_Git’ and open Git Bash within it.

  2. Update your Git Global Config Settings.

    • Set your user.name value to your name. Make sure the name is wrapped in quotes if there is a space in it!

    • Set your user.email value to your email address. As the email address has no spaces in it, is does not need to be wrapped in quotes.

  3. Initialise a new Git repository (repo) within the ‘Intro_to_Git’ directory.

Question 1

  1. Create a folder on your desktop called ‘Intro_to_Git’ and open Git Bash within it.

Right click on your desktop and select New / Folder, and name it Intro_to_Git. Open up the folder, right click and select Git Bash Here.

Question 2

  1. Update your Git Global Config Settings.
$ git config --global user.name "Your name"
$ git config --global user.email an.appropriate.email@domain.com

Question 3

  1. Initialise a new Git repository (repo) within the directory.
$ git init

Reuse

Open Government Licence 3.0