Class 2B. Introduction to the Terminal (Command line) and Git#

CommandPrompt CommandPrompt

Lecture Outline#

1. Announcements (5 mins)

2. The Command Line (30 mins)

3. Git and Version Control (15 mins)

Part 1. Announcements (5 mins)#

  • Lab 1 will be due Wednesday January 18, 2023 at 6 PM

  • My office hours will be for 60 minutes after class on Mondays in EME 0050 and Fridays from 1-2 PM via Zoom. You can also ask for help anytime on Ed Discussion, as well as visiting any of the labs.

  • Lab sessions start this week, and will not be recorded.

  • Here are some keyboard shortcuts in VS Code to do some cool things:

Action

Shortcut on Mac

Shortcut on Windows

Insert additional cursor

Option + Click

Alt + Click

Insert cursor at end of each selected line

Shift + Option + I

Shift + Alt + I

Select all occurrences of current selection

Shift + Cmd + L

Shift + Ctrl + L

Select all occurrences of current word

Cmd + F2

Ctrl + F2

Add selection to next Find match

Cmd + D

Ctrl + D

Select current Line

Cmd + L

Ctrl + L

There are dozens more shortcuts, you can find the shortcuts here for:

Part 2. The Command Line (Terminal) (20 mins)#

If you would like some additional notes and a much deeper explanation of the command line (also known as the Terminal), I suggest the notes from Software Carpentry! They are excellent.

In particular, I suggest “Introducing the shell”, “Navigating Files and Directories”, and “Working with Files and Directories”

What is the Command Line?#

The command line is the text interface to the computer that accepts commands that the computer will execute. These commands include:

  • starting programs

  • navigating directories and manipulating files

  • searching, sorting, and editing text files

  • system and environment configuration

Why learn command line?#

The command line is the text interface to the computer. Understanding the command line allows you to interact with the computer in ways that you often cannot with the graphical user interface (GUI).

The command line is commonly used for scripting and automation of tasks and when accessing remote systems. It will also be useful to run programs that make use of the command line (eg. github).

Why use command line?#

The command line is part of the operating system (OS), which is software that manages your computer including all devices and programs.

  • Common operating systems include Windows, Mac OS, and Linux/Unix.

  • Some commands will be OS specific

You might be wondering why we would ever prefer command line over using the graphical user interface (GUI).

  • Certain tools may only be available to command line.

  • Sometimes command line is faster.

Command Line on Windows#

The command line on Windows dates back to the original Microsoft operating system called DOS (Disk Operating System) in 1981.

This command line interface is still part of all modern Windows operating systems and is accessible as the “Command Prompt”.

I am requiring you use GitBash instead of the default Windows Command Prompt.

Windows Terminal#

This is what the GitBash “Terminal” looks like. From here on out, whenever we say “Terminal”, we mean “GitBash on Windows”, “Terminal on macOS and Ubuntu”

CommandPrompt
  • The directory separator is a forward slash / for Macs/Ubuntu, and the opposite pointing slash on Windows \

  • However if you use GitBash, this should preserve your sanity and you should be able to use forward slash on Windows as well

  • In fact, many of the macOS/Ubuntu commands will also work in GitBash, but for completeness we will also share the other commands

Image source: Git for windows

Mac OS Command Line#

The command line for Mac OS uses the same commands as Linux. It can be opened using Finder then Utilities then Terminal.

CommandPrompt

Alternatively, we could type Cmd + spacebar, then type “Terminal” and press ENTER

Entering Commands#

Enter a command at a prompt. The prompt may be a > or a $ or customized by the user.

Press ENTER to execute the command.

Commands are case-insensitive on both Mac/Linux and Windows. Running commands CommandPrompt

Here are some useful Terminal Commands

  • cd : change directory

  • pwd : print working directory

  • mkdir: make directory

  • ls: list directory contents

  • rm: Delete a file

  • rm -r: Delete a directory

  • touch: create an empty file with a specific name

  • code: edit the file in the VS Code editor

Command-line Summary#

  • Briefly explain components of the Install Stack

  • Define command line and list some of its uses

  • Explain the purpose of an operating system

  • Know how to open the command line window on macOS and Windows

  • Enter commands in the Terminal and stop them

  • Define: file system, folder, file

  • Explain the difference between an absolute and relative path

  • Use command line shortcuts to save time

  • Be able to cancel a command (Ctrl+C)

  • Describe the full process of submitting a lab using GitHub and GitHub Classroom

Tips and Tricks with the Terminal#

  • Use the pwd command to find out where you are in the Terminal at any point!

  • Use “Up” and “Down” arrow keys to cycle through previously used commands.

    • This is a huge time saver!

  • Use Tab completion!!

    • Press tab to see available options

  • Use * to autocomplete partial matches

Conclusion#

  • The command line is the text interface to the computer that accepts commands that the computer will execute including running programs, manipulating files, and running scripts.

  • The command line allows for automation and more control than may be available in the user interface. It may also be the only way to interact with the machine if connecting via SSH.

  • The command environment allows for redirecting the standard input and output using input/output redirection and pipes.

  • You need to PRACTICE PRACTICE PRACTICE working in the Terminal: to get yourself used to it, I suggest not using the Finder/Explorer on your computer and use the Terminal for everything.

    • open will open a file in the default application on macOS.

      • open . will open the file in Finder.

      • explorer.exe . will open the folder in Explorer

Activity: Introduction to Git and Version Control (15 mins)#

  • Log in to github.com

  • Create a new repository (empty), make sure to initialize with a README.md

  • Clone that repository locally

    • git clone https://github.com/demofiras/gitdemo2022.git

  • Make a change on Github.com to README.md

    • Use the web editor

  • Make a change locally to README.md

    • Use VS Code to edit your README file

  • Add and commit your change to the repository

    • git add . ; git commit -m 'change to README

      • Note that the ; allows you to put multiple commands in a single statement!

  • Try to push your local change

    • git push

    • You should see an error asking you to run git pull!

    • Run git pull, it should fail saying fast forwarding (the default) is not possible.

    • Next, look at the manual fir git pull to see if there’s a way to pull without doing a “fast-forward”

    • There is: git pull no-ff “no-ff” means no fast-forward

    • It will report there are merge conflicts that you will need to resolve

  • Deal with the conflict

    • I selected “Accept both changes”, but depending on the changes you made, another option may be better, feel free to experiment.

  • Push changes

    • git push -f

That’s it! See you Next Class!#

Guide to the operating system for macOS users#

IFrame(
    "https://www.youtube-nocookie.com/embed/RX160bcMFuA", width="100%", height="500px"
)
## Guide to the operating system for Windows 10 users
IFrame(
    "https://www.youtube-nocookie.com/embed/egK5BKEn87c", width="100%", height="500px"
)