00. Terminal Basics

01. Git & GitHub


Terminal Basics

The terminal is a program that gives you a command line interface or (CLI) to the files and programs on your computer. You are probably more familiar with a graphical user interface or (GUI), which entails using the mouse to open files and folders. But you can do the same things via the command line using the keyboard to input text-based commands.

Understanding the prompt

  • The prompt is what a terminal will display before you enter a command. On the school comptuers, it will look like this:
    • dw@cslab1-4:~$
  • There is a lot of information packed into that line of text. You can decipher it as:
    • USERNAME@COMPUTER:CURRENT_DIRECTORY$
    • It tells you who is currently logged in, what computer you are on and what directory is currently open.
      • The : separates the computer name from the current directory.
      • The ~ is a shorcut for the user’s home directory. (Without it my prompt would look like this instead: dw@cslab1-4:/home/support/dw$)
      • The $ is there to signify the end of the prompt. Whenever you see instructions from me that start with a $, that means I am providing you with terminal based commands.

Using programs via the command line

  • In a GUI, you open programs by clicking on an icon. Notably, you need to find that icon in order to click on it.
  • In a CLI, if the program is installed in a normal location, you only need to type the name of the program at the prompt and hit enter. For example, you can launch processing with $ processing or firefox with $ firefox. Both of those programs do not run in the terminal, but can still be launched from it.
  • Programs that are meant to be run in the terminal are often referred to as commands.
  • The 3 Most Important Command Line Programs
    • $ ls: List the files in the current directory
    • $ cd DIRECTORY: Change the current directory.
    • $ cat FILE: View the contents of a file in the Terminal. This works very well for text-based files (.pde, .md, .txt etc.). But not for things like images.

Like Real Estate, It’s All About Location

  • You’ll notice that the first 2 most important command line programs listed above deal with the “current directory”. In a CLI, the current directory is used at the starting point for any command you enter. For example, if you want to open a file and it is in the current directory, you just need to enter $ open FILENAME. If the file is in a different directory, then you need to provide more information about the file’s location.
  • You can see the current directory in your command prompt, but you can also run $ pwd to see the full path to your current directory as well. We often say that a Terminal session is in the current directory.
  • Using cd to change directories.
    • You can manipulate your current directory with the cd command (short for change directory).
    • If you want to change into a directory that is inside your current directory, you can enter $ cd SUB_DIRECTORY. If the directory you want to change into is not in the current directory, you will have to provide more information about the location of that directory.
    • If you are in a directory, and want to go back up a level to the parent directory, you can use .. like so: $ cd ...
    • If you want to go back to your home directory, you can enter cd without any arguments (i.e. $ cd).
Back to top

Git and GitHub

Some background information:

  • Git is a program designed to help people keep track of changes to their work over time, sync copies of the same work on multiple computers, and help multiple people work on the same project. When you use git, you put your work in a specific folder called a repository and use git commands to maintain that repository.
    • All of the commands that you enter on the terminal are specifically git commands.
  • GitHub is a website that (among other things) provides a central place to store git repositories. You do not need to host your repository on GitHub to use git, but it makes it easier to keep things in sync across multiple computers (like at school and home).
  • All of the git-related commands should be entered in a command line environment. On the school computers, you access the command line via the Terminal program. At home:
    • If you have a Macintosh or Linux computer, you also have a program called Terminal, it will work the same way Terminal works at school.
    • If you have a Windows computer, install git for windows. This will install a program called Git Bash. You can use Git Bash the same way you use Terminal at school.
  • When working in the terminal, make sure you are in the correct directory. You can always see the current directory you are in by using the pwd (short for print working directory) command in the Terminal or Git Bash.

Git vocabulary:

  • Repository: A directory containing work to be tracked by git.
  • GitHub: A website that provides a central place to store git repositories.
  • Clone : Make a copy of a git repository on a computer.
    • Once a repository is cloned, it will be forever linked to any other copies (including the one on GitHub).
    • The cloned copy of a repository on a specific computer is called the local copy.
  • Add : Tell git to keep track of a new file.
    • All new files must be added, or git will not recognize them.
  • Commit : Keep track of any new changes you have made to files within the repository.
    • Committing changes only effects the local copy of the repository.
  • Push : Upload any committed changes to GitHub.
  • Pull : Download any changes from GitHub to your local copy of a repository.

Installing GIT At Home

Mac and linux machines probably have git installed already.

  • To test, open your terminal program, and type $ git. If git is installed, you should see a list of various options for the git command.

Windows users will want to install git and the shell gitbash from here: https://gitforwindows.org/ This includes git, gitbash, and has shell integration so that you can right-click on a folder in Windows Explorer to access gitbash at that location!.

  • Although it comes with GitGUI, please learn to use the terminal commands until you understand exactly what you are doing.

Configure git on each machine you plan on using

Configure your local git program using these commands. Important: Use your name and your git related email.

  git config --global user.name "John Doe"
  git config --global user.email johndoe@example.com
  git config --global core.editor nano

Git/GitHub Workflow

  • Make a repository on GitHub (for class, this will be done for you when you accept a GitHub classroom assignment). Get the ssh link from the green code button on the repository webpage.
  • Once Per Computer
    • Clone the repository via the command line (either in terminal or gitbash).
      • $ git clone REPOSITORY_SSH_LINK
    • Once you’ve done this, you will have a copy of the repository on your computer. It will be an exact copy of the repository at the time of cloning. It will not keep up to date automatically.
  • Every time you work in your repository
    1. Before doing anything else, get any potential updates from GitHub’s version of the repository. Every if you’re 100% certain there are no changes, do this, it won’t hurt.
      • $ git pull
    2. Whenever you create a new file, let git know it should keep track of it by adding it:
      • $ git add FILE
      • If you don’t do this, git will ignore the file entirely.
    3. When you are done working, commit your changes, and then push them to GitHub.
      • $ git commit -am "RELEVANT MESSAGE"
      • $ git push
    4. You should run all these commands from within the directory for your repository. See the Terminal Basics notes for more information on keeping track of your current directory.
Back to top