Git

From UCT EE Wiki
Jump to navigation Jump to search

Overview[edit]

Throughout the course you will be required to write and submit code and other files to an online git repository, as well as submitting it through Vula. Git is a popular version control application that allows you to keep a record of changes you have made to files over time. Additionally, by using a version controlled repository that is accessible to other collaborators, it provides a powerful way for many people to collectively build a larger system. Almost all open source projects use a version control system to enable them to work with anyone else around the world in a structured and organised manner. There are many platforms that offer free hosting services where you are able to share repositories, Github, and Gitlab are two such options. Making an account on either of these (or a similar alternative) allows you to:

  1. keep track of your own projects and the changes you make to them,
  2. work collaboratively with teammates if you choose to share your repositories with them, and
  3. is an increasingly common way of sharing a portfolio of your prior work with potential employers.

Git can be intimidating in the beginning, but it becomes invaluable as you progress in software development. This page has a great guide you can follow to get well acquainted.

GitHub and the GitHub Education Pack[edit]

Git is a local tool. To use online backups, it’s recommended to use a remote (online) repository management tool. GitHub is recommended because, as a student, there are many benefits which you can access. See https://education.github.com/pack to sign up.

A Quick Git Get Go[edit]

This sub section has a bunch of useful “recipes” to follow that will be common throughout your experience with git. You can also view these resources to help learn git:


Creating a GitHub Account and Configuring your Computer[edit]

  • Start by creating a GitHub account

  • Install git on your computer
    Lab computers already have git installed.
    If you’re using a Linux based system, this can be as simple as sudo apt-get install git
    If you’re using Windows, you need to download and use an installer.

  • Once git is installed, run the following commands:
    Note: Do not do this on the lab computers. If you are on a lab computer, rather set the user.name and user.email parameters from within the created git repository, and do not use the --global flag. Only use the global flag if you’re on your own system, such as your own PC or your Pi.

    $ git config --global user.name "Your Name"
    $ git config --global user.email "github email address"
    
  • Git is now configured

Creating a New Project[edit]

Git consists of three primary stages: Untracked, staged and committed. Untracked files are not tracked by the repository. Staged files are files staged for commit but not yet committed. Committed files are “saved” to git. On your local system:

  • Create a folder and enter into it
  • Run git init
  • Create a new text file, for example “test.txt”
  • Run git status
  • You will see there is an untracked file. Add it to git by running git add test.txt
  • It is possible to add all untracked files by running git add .
  • If you run git status again, you will see that “test.txt” has been staged, but not yet committed. Commit test.txt by running git commit -m "Created test.txt". The -m flag is to include a git commit message. It’s useful to use these messages to explain what has changed in this commit.

Linking GitHub and your Local Project[edit]

On GitHub, create a new repository and give it a meaningful name and description. Take note of the link (something like https://github.com/<username>/<project-name>.git)

GitHub gives instructions on how to push an existing repository from the command line, but for completeness sake the commands are included here:

$ git remote add origin https://github.com/<username>/<project-name>.git
$ git push -u origin master    

If you refresh the GitHub page, you should now see your files and commits.

Understanding .gitignore[edit]

Related SW Carpentry link: Ignoring Things

You may have seen the option to add a .gitginore when creating a new repository on GitHub. This is used to get Git to ignore certain files, such as interim or raw data files.

Fetching an Existing Git Repository[edit]

Oftentimes you will need to fetch initial files and templates that have been created for you. If the project is on GitHub, you could navigate to GitHub, download a compressed folder, move it to the system you want to work on, unzip it, and then work with the files, it’s much easier to just run the following command:

$ git clone <GithubProjectUrl>.git