Coventry University Logo
4061CEM - Programming and Algorithms 1
  • Git Commands
    9

Git Commands

Before you attempt this activity, it is best that you review lecture video three from week one. You can find the video and slides on the module page.

For this activity, you will be using the Terminal window in a Linux environment. A shortcut to open the terminal window is Ctrl+Alt+T. During this lab activity, you will be presented with various Linux commands to enter into the terminal console. These will be shown in the following format:

$ command

The dollar ($ ) symbol part of the command should not be typed in. This is included to identify that this type of command is to be executed in a shell/terminal window. For example, if you are presented with:

$ mkdir 4061CEM

In the terminal window you shall enter mkdir 4061CEM without the dollar symbol. In some cases, you may be presented with a command, and then an output directly below it. This may look something like:

$ pwd
  /home/ian

In this instance, the pwd is the command that was entered into the terminal window and the /home/ian below (and indented) is the outcome of the command. In most cases, the outcome that follows a command may not match up to the result that you achieve; this is to be expected.

Creating a Private Repository

Before you can begin using the Git commands in the terminal window, you need to create a repository on the Coventry University GitHub service. You can access the service at the following URL:

You will need to sign in using your university credentials, these are:

Username: <student_username> 
Password: <your_password_for_email>

When logging into this service, you do not need to enter your e-mail address. You only need to enter the prefix of your e-mail. For example ab6459@coventry.ac.uk would become ab6459.

Once you have logged into the website, on the left-hand side of the page, there is a green button with the text New. Clicking on this button will direct you to a new page to create a repository. On this page, you will need to enter/check the following details:

  • Set the name of the repository as 4061CEM
  • Ensure that the repository is Private

Once you have entered these details, you can click on the button with the text create repository. This will create a private repository which you can ue for the rest of this guide. You may also want to use this repository for any lab tasks you may partake in for this module.

You will be expected to use Git for your coursework. It is best to ensure you are familiar with how Git and the various commands work, so you can submit your coursework in an appropriate manner. You will also want to create a separate private repository for both of the coursework.

Adding a Collaborator

There may be instances where you will work as a group, and therefore these group members will need access to the repository. There will also be times when you are expected to add staff members of a module to the repository to assist in marking of coursework or lab tasks.

You are not required to action these steps right-now. However, when it comes to working as a group and submitting your assignments for coursework one and two, you will need to complete this part.

To add a user to your repository, you can follow these steps:

  1. Click on Settings on the repository page you want to add the user to
  2. Click on Collaborators on the left-hand side of the page
  3. Search for the user you wish to add by their university username
    • i.e. Dr Ian Cornelius would be ab6459

When you add a collaborator to your repository, it is worth keeping in mind that the user will have access to viewing and making changes to the work. Therefore, only add those users you want to see the work. Do not provide access to the repository if you would not be happy for the person to see your coursework solution.

Cloning an Online Repository to the Local Machine

Before you begin cloning/copying a repository to your local machine, it is first best to decide whereabouts you would like your projects to be located. For example, you may want to create a folder called projects in your home directory.

In your Linux terminal, create a directory called projects in the home directory. This is where you will clone/copy the online repository to. Once the directory has been created, enter this directory.

If you get stuck on creating the directory and entering it, you may want to revisit the lab on Linux Commands.

Inside this directory, you can clone the online repository using the command git clone. To do this, you can use the following command:

$ git clone https://github.coventry.ac.uk/YOUR_USERNAME/4061CEM.git

Ensure to replace the YOUR_USERNAME part of the URL with your university username. If there is any confusion, it is shown on the page of the repository when you created it.

When you first begin the cloning process it will ask for your credentials. Enter your details, and it will begin cloning the repository to the local machine. It will throw a warning that you have cloned an empty repository - you can ignore this. Once the cloning process has finished, you want to enter the directory.

Setting up your User E-mail and Name

Before you can begin submitting any files to the repository; Git requires to know the users name and e-mail for each commit. To add these details, you can use the following commands:

 $ git config --global user.email "YOUR_USERNAME@uni.coventry.ac.uk"
 $ git config --global user.name "YOUR_NAME"

You will want to replace the YOUR_USERNAME with your username, and YOUR_NAME with your name (inclusive of surname/family name). It is expected that you will use your Coventry University e-mail address and your actual name; this will make it easier for the module team to track changes.

Creating Directories for the Repository

Inside the directory for your repository, you will now create folders for each lab. Instead of manually using the mkdir command for each week (there are twelve weeks in a semester), you can shortcut this by using the following command:

$ mkdir Labs{01..12..1}

The commands above use the brace expansion in terminal, which is a for-loop to create multiple directories. For example, the command mkdir Labs{01..12..1} will create directories with the names Lab01, Lab02, Lab03 ... Lab12 inside your repository.

Git will not track these empty folders, and therefore to bypass this you will place a text-file in each folder named author. You can do this using the echo command and > symbol to redirect the output to a file:

$ echo "YOUR_NAME" > Labs01/author

You want to repeat this command for each folder you have created in this repository.

Adding the Folders/Files to the Remote Repository

With the directories now populated with some files, you can now copy it from the local repository to your remote repository. You have to specifically tell Git when to save a version, until you perform this task your remote repository will remain empty. Before we add the files to the remote repository, you can check the status of your local repository by doing the following:

$ git status

Which will provide the following output:

$ git status
  On branch master

  No commits yet

  Untracked files:
    (use "git add <file>..." to include in what will be committed)
        Labs01/
        Labs02/
        Labs03/
        Labs04/
        Labs05/
        Labs06/
        Labs07/
        Labs08/
        Labs09/
        Labs10/
        Labs11/
        Labs12/

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

The untracked files indicate that these files will not be tracked by Git. Therefore, you will need to tell Git to track a file by using the git add command. To do this for all the files in each folder we can use this command:

$ git add Lab*/author

The wild-card (*) has been used to select all Lab folders. Re-running the git status command will now provide the following output:

$ git add Lab*/author
  On branch master

  No commits yet

  Changes to be committed:
    (use "git rm --cached <file>..." to unstage)
        new file:   Labs01/author
        new file:   Labs02/author
        new file:   Labs03/author
        new file:   Labs04/author
        new file:   Labs05/author
        new file:   Labs06/author
        new file:   Labs07/author
        new file:   Labs08/author
        new file:   Labs09/author
        new file:   Labs10/author
        new file:   Labs11/author
        new file:   Labs12/author

You will now see that the files will be tracked by Git. However, you will now need to commit these changes to the local repository. This can be done using the git commit command:

$ git commit -m "Initialising repository with directories"

Note that here you make use of -m flag/option, and it is optional, however it is used to specify a commit message. These are useful to describe any changes that have been made, and you are encouraged to use these for each commit you make. Having a descriptive message will make it easier to find the right place in your project history if something was to go wrong.

The commit command will save a snapshot of all the files that Git is currently tracking. This will save the snapshot to your local repository. In order to make these changes to the remote repository, you need to use the git push command:

$ git push

The command will ask for your username and password for the GitHub service, enter your appropriate details.

To assist in activities later, create a new document called hello_4061cem.txt and add the following text:

Hello 4061CEM, this is a sample file.

Once the file has been created, add the new file to the Git repository and check it in.

Checking the Difference between Files

You will now go back and edit the previous file: hello_4061cem.txt and remove the text:

, this is a sample file.

The text file show now have the following content:

$ less hello_4061cem.txt
  Hello 4061CEM.
  hello_4061cem.txt (END)

When you run the git status command, you will be met with the following output:

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

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

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

You can see on line eight, that the following text appears: modified: hello_4061cem.txt. This indicates that the file has been modified; and tells you the next commands you may want to use to commit these local changes. You will know what has been modified, but if you are working with team members, and you are unsure what has been modified you can use the command git diff to view the changes made to a file.

$ git diff
  git --diff a/hello_4061cem.txt b/hello_4061cem.txt
  index c93c41c..adc56c3 100644
  --- a/hello_4061cem.txt
  +++ b/hello_4061cem.txt
  @@ -1 +1 @@
  -Hello 4061CEM, this is a sample file.
  +Hello 4061CEM.

From the snippet above, you can see that lines seven and eight explain the modifications made to the file. In this instance -Hello 4061CEM, this is a sample file. means that the text was removed from the file and was replaced by +Hello 4061CEM.. The respective symbols (- and +) represent what was removed and added to the file.

In order to make these changes to your remote repository, you will need to commit the changes and then push them to the server:

$ git commit -am "Removed a line from the text file"
$ git push

The -am flag/option tells the commit command to automatically stage files that have been modified and deleted, but any new files are not to be affected.

Moving and Copying Files

Git provide their own commands when it comes to moving or copying files. It is good practice using these commands instead of the Unix equivalents, as they preserve version history. For example, by changing the filename, you could see its history back through its original filename.

Moving files with Git, you can use:

$ git mv filename destination_filename

Branching a Repository

When working on a new feature of a project, it is good practice using a separate branch. This can be done by checking out a branch based upon the master:

 $ git checkout -b new-branch

The flag/option -b in the checkout command tells Git to create a new branch, if it does not exist already. Within this branch, you can now make changes to the source-code or files without making changes to the original files or master branch as it is known.

On this new branch, lets modify the hello_4061cem.txt file. In this file, lets add some new text:

Working on a new branch means that any changes I make, will not show up on the original master branch.

The file should now look like:

$ less hello_4061cem.txt
  Hello 4061CEM.
  Working on a new branch means that any changes I make, will not show up on the original master branch.
  hello_4061cem.txt (END)

On this branch you can use the usual add, commit and push commands; building upon your feature with as many commits as you like. You may however, like to push changes to the remote repository, and under the new branch. This can be achieved by:

$ git add hello_4061cem.txt
$ git commit -m "Added a new line of text to the file"
$ git push -u origin new-branch

This command will push changes in the branch to the remote repository. The -u flag/option on the command adds the branch as remote tracking. Once a tracking branch has been set up, the git push command can be invoked without any parameters to automatically push this branch to the remote repository.

To see how these changes have been executed, navigate to your repository online at the following URL:

https://github.coventry.ac.uk/YOUR_USERNAME/4061CEM/

Ensure that you change the YOUR_USERNAME part of the URL. On this page you can see that the master branch selected as default on the left-hand side of the page. Click on this drop-down box, and you can see your newly created branch new-branch. Click on the file hello_4061cem.txt and you will see the new line of text added to this file. If you go back to the master branch, you can see that this new line of text is not present.

Merging Branches

If this new-feature you have been working on in a separate branch is successful, and does not break the original functionality of your code on the master branch; then you can merge the two branches together. In order to merge the branches together, you need to switch back to your master branch:

$ git checkout master

You can then use the merge command to merge new-branch and master together:

$ git merge new-branch
  Updating 25cfef9..3976a3e
  Fast-forward
  hello_4061cem.txt | 1 +
  1 file changed, 1 insertion(+)

Finally, once the two branches have been merged together you can make a push to the remote repository.

Deleting a Branch

You may notice on the online repository page that the branch is still available. If you do not need to work on the branch anymore, then you can delete it (locally) using the following command:

$ git branch -d new-branch

However, to delete a remote branch you need to use the push command and the --delete option:

$ git push origin --delete new-branch
  Username for 'https://github.coventry.ac.uk': ab6459
  Password for 'https://ab6459@github.coventry.ac.uk':
  To https://github.coventry.ac.uk/ab6459/4061CEM_2122.git
      - [deleted]         new-branch

Reverting to Another Branch

Sometimes mistakes happen, and you need to undo that change you made. Luckily, Git has an undo command, known as revert. However, it is not your traditional undo operation. Instead of removing the bad commit from the projects' history; it instead inverts the changes that were introduced by the commit and appends these to a new commit. This method of doing it means that Git does not lose history.

Looking at the history of commits made, using the log command, you will have:

$ git log --oneline
  3976a3e (HEAD -> master, origin/master) Added a new line of text to the file
  25cfef9 Removed a line from the text file
  916e061 Added file to test differences
  46a18c3 Initialising repository with directories

Note

The commit ID may be different to those shown above; and this is to be expected. Ensure to select the commit ID that refers to your output.

Selecting commit ID 916e061, you can revert to this revision with the following command:

$ git rever 916e061

Once the command has been executed, it should revert you back to the initial commit where the file-structure was created, alongside with the author files.

Conclusion

You have now reached the end of this tutorial. The outcome of this tutorial is the ability for you to create a GitHub repository, and using the necessary commands to clone to your local machine and push/make changes.

Any Issues or Errors?

If you have spotted any errors or issues within this tutorial, you can e-mail Dr Ian Cornelius. Ensure to include in your message a description of the error/issue and a possible resolution.