Skip to main content

Git Basics

Work In Progress
This page is a work in progress, accuracy of the content is not guaranteed and is subject to change at any time.

Installing Git

To install Git, follow the instructions on the Git website for your operating system.

Configuring Git

Once Git is installed, you will need to configure it with your name and email address. This information is used to identify you as the author of changes. Run the following commands in your terminal of choice, replacing values as appropriate:

git config --global user.name "NAME"
git config --global user.email "EMAIL"

Creating a repository

To create a new repository, navigate to the directory you wish to create the repository in and run the following command:

git init

Cloning a repository

In practice, a repository is created once and pushed to a remote. In future rather than creating a new repository, it is instead cloned via the git clone command. The exact command can usually be found within the remote repository's web interface.

Committing changes

To commit changes to a repository, you must first stage the changes. This is done via the git add command, with the relative file path of the changes. Once the changes are staged, you can commit them with the git commit command. The -m flag is used to provide a commit message. The following command stages and commits all changed files:

git add .
git commit -m "Commit message"
tip

Use VSCodes inbuilt Git UI to more easily stage and commit changes without needing to use the command line.

A good commit is as follows:

  • Has a descriptive commit message
    • Describes what the changes are
    • Uses imperative mood (e.g. "Fix issue with account creation" rather than "Fixed issue with account creation")
    • Is concise
  • Is atomic
    • Contains a working change; applying this commit should not break functionality
    • Contains a single, coherent change; the commit should not contain multiple unrelated changes
      • This doesn't mean a single file or line change, but rather a single logical change
  • Is performed frequently
    • Commits should be made regularly, rather than in large batches