Skip to main content
Table of contents

Using the terminal and git

The terminal

The command line is a text interface for your computer. Just like Windows Explorer on a PC or Finder on a Mac, it lets you navigate through the files and folders of your computer, but it’s completely text-based.

The command line can seem unfamiliar and scary, but it’s really a different way of interacting with your computer. This tutorial only covers safe commands that will not do anything bad to your computer, even if you get them wrong.

Terminal is only available in Mac OS X. For Windows, use Git Bash.

Once you’ve opened your terminal you should see a window.

Screenshot of the Terminal window. The text says 'Last login: Sat May 18 11:08:55 on ttys002 ~$'

Do not worry if the text in yours is a little different. It does not matter.

To run a command, type the words and press Enter.

Try running:

pwd

pwd or print working directory

The pwd command prints to the command line the current directory (another name for folder) you are in. If you just opened up your terminal, you are probably in your ‘home’ directory, and you should get an output similar to this:

/Users/your-username

So your current ‘working directory’ is /Users/your-username.

cd or change directory

The cd command allows you to move between directories. You tell cd which directory to move to by putting the path after the cd, like this:

cd Desktop

This moves you into the Desktop directory.

You can go up a directory level:

cd ..

You can also go to your home directory.

cd ~

ls or list

The ls command lists the files in a directory:

ls

This should print a list of the files and folders inside the working directory. If you’re in your home directory, you’ll probably see directories like Applications, Desktop, Documents and Downloads.

Terminal shortcuts

Use Up and Down to go through previous commands.

Select Ctrl and C together to cancel a running command.

Select Command and K together to clear your terminal. This does not stop a running command. It removes lines of content from view.

Select Tab to autocomplete a file or folder name. You may need to press it a second time if there are no unambiguous matches.

Getting things wrong in the terminal

If you type a command that the command line does not understand, it will show you an error message. Do not worry if you see one of these. Have a look at the command you wrote and see if you can work out what was wrong.

Try the following for example:

whargleblargle

You should see an error message like:

-bash: whargleblargle: command not found

Common Git commands

Create a repository

A Git repository is a virtual storage of your project. It allows you to save versions of your code, which you can access when needed.

Create a new local repository with the specified name:

git init PROJECT-NAME

Download a project and its entire version history:

git clone PROJECT-URL

Make changes

List all new or modified files to be committed:

git status

Snapshot the file in preparation for versioning:

git add FILE-NAME

Snapshot multiple files:

git add .

Record a snapshot of your file (a commit):

git commit -m MESSAGE-THAT-EXPLAINS-YOUR-COMMIT

Synchronise local and remote repos

Upload all local branch commits to GitHub:

git push REMOTE-BRANCH-NAME BRANCH-NAME

If pushing to a master branch from a local repo (a folder on your device), you can write the above as:

git push origin master

Download history from a remote directory and incorporate the changes in your local version:

git pull

Branches

Git branches are a pointer to a snapshot of your changes. If you want to add a new feature or fix a bug, you can create a new branch to encapsulate your changes.

List all local branches in the current repository:

git branch

Create a new branch:

git branch BRANCH-NAME

Switch to the specified branch and update working directory:

git checkout BRANCH-NAME

Combine the specified branch’s history into the current branch:

git merge BRANCH-NAME