Practical: Basic git usage from the command-line

We use git version control system, with automatic checks on submitted versions, extensively in our courses at Heriot-Watt. Courses will give you an intro on how to use git from different IDEs. The role of this section of the Linux Introduction is to familiarise yourself with basic git usage from the command line, which does not depend on a specific IDE.

TipGit cheat sheet
 

Keep a git cheat sheet or quick-reference close-by (browser window or printout) for quick lookup. Here is a useful git cheat sheet.

NoteRegistering with gitlab-student / Uploading your SSH key
 

Before you start working with the gitlab-student, which is our (HWU) instance of gitlab that we use in courses, you need to register with gitlab (see this video) and you need to upload your SSH key (see this video) , so that you can interact without using a password.

Typically, you will start an exercise with a link to a gitlab (or github) repository that holds, e.g. sample code, of an exercise to complete. This comes in the form of a URL that ends with .git. To get the contents of the repository onto your own machine you need to clone the repository like this:


  $ git clone git@gitlab-student.macs.hw.ac.uk:linuxintro/hello-python.git
  

This command will create a copy of the entire repository and put it into a subdir matching the name given. Since git is a peer-to-peer version control system, there is no master copy that you checkout or checking. All instances of the repos act as peers and you can synchronise between them by performing push and pull operations.

To work with the cloned repository, enter the newly created directory, and use the remote, to see which repo your instance is connected to. This is important, because in a lab exercise setup, you need to be connected with a your forked version of the original repo.


  $ git remote -v 
  

You should see a response like the one below, which tells you to which point on the gitlab-student server your local repository is connected to.


  origin	git@gitlab-student.macs.hw.ac.uk:linuxintro/hello-python.git (fetch)
  origin	git@gitlab-student.macs.hw.ac.uk:linuxintro/hello-python.git (push)
  

In our example, we have a repo with a Hello World program, written in Python. The details of program or language are not important for this tutorial. Here is the contents of the directory that you should see:


  $ ls
  hello.py  README.md
    

You can execute the code by typing (you need Python 3 installed on your machine):


  $ python3 hello.py
  

NoteExercise
 

As an exercise, change the Python code, to print a message Hello NAME, where NAME should be your first name. You only need to change the string in the Python program, not the code itself. Refer to the section on editors on how to call an editor from the command line. Test the code by running it. Once done, push the code to the repository.

Once you are done with edits to your files, you need to notify git that you have made changes that should be recorded in the (local) repository. In our example, you should have modified hello.py and you need to add this file to the git repo:


  $ git add hello.py
  

Then, you need to commit the change to the repository. Note that at this point this changes only the local repository not the remote one. Use a message (here "Commit message") to document the change that you made to the files that you added in the previous step.


  $ git commit -m "Commit message" hello.py
  

If this completes the exercise, then you are ready to upload the changes into the remote repository (the one shown with git remote above), which should be your fork of the main repo for this lab exercise.


      $ git push .
    

If there are changes to the main repo, you use git pull to get these changes. In the lab exercise example this shouldn't be necessary. A possible case would be where more sample files are uploaded, or there is a modification to the coursework spec.


  $ git pull
  

If you need to start from scratch, you can create an empty repository using the git init command. This would be useful to start e.g. a repo for a group project, where you don't have a sample repo to clone. First generate an new directory, then do a git init:


    $ mkdir myRepo
    $ cd myRepo
    $ git init
    
  

Further reading

There are a lot of good introductions to git online available. Below are some links that might be useful: