Skip to content

Instantly share code, notes, and snippets.

@chrisdaaz
Last active April 17, 2022 19:07
Show Gist options
  • Save chrisdaaz/665bcbd40cd983189efac45790e298e8 to your computer and use it in GitHub Desktop.
Save chrisdaaz/665bcbd40cd983189efac45790e298e8 to your computer and use it in GitHub Desktop.

Setting Up Your Computer

These instructions provide an opinionated method for setting up your computer to use Git and GitHub for this course. These instructions assume that you are new to Git, terminals, and text editors. You are absolutely welcome to disregard my suggestions if you're comfortable with command line interfaces and text editors. Experienced users can skim this page and follow along beginning at the "Your First Repo" section.

Learning Git and GitHub requires setting up accounts and installing some free software packages. This section steps you through the process of setting up your computer. If you're comfortable using text editors and terminals, you can disregard my recommendations.

GitHub

Create a GitHub account: Visit https://github.com/ and sign up for a free account.

  • You will need an email address for an account

Text Editor

Install Visual Studio Code: We will need this to create and edit plain text files. You can use any text editor you like, but I recommend Visual Studio Code for beginners. VS Code, like other Git graphical user interface clients, offers several features and extensions that make it easier to run Git commands as part of our writing and editing workflow.

  • There are default text editors on Windows (Notepad) and MacOS (TextEdit). These are fine for very basic text file editing, but modern text editors (like Visual Studio Code) have a lot of features that make working with plain text a lot easier.

Git

Check to see if you already have Git installed on your machine.

  • On macOS/ Linux, press Command + Space and type “terminal”
  • On Windows, click the search icon in the taskbar and type “cmd”

This will open your terminal. In the terminal, type git --version. If you get a version number, Git is already installed, any other response means that you do not have Git installed.

Installing Git on macOS (and Linux)

I recommend macOS (and Linux) users to have Homebrew installed on your machine. Homebrew is a software package manager. It make open source command line software installation and upgrades very easy. You can follow the installation instructions on the website: https://brew.sh/

Once you have Homebrew installed, installing Git is as simple as running this command in your Terminal: brew install git

You can now skip ahead to the Git configuration section.

Installing Git on Windows

If you're interested, here's my personal setup: I use a Dell laptop with Windows 10 and Windows Subsystem for Linux (WSL). WSL gives you a Unix/Bash-based terminal with access to a Linux OS distribution from the command line. I use Ubuntu 20.04 as my Linux distribution. This lets me use Windows 10 for normal, every day computing, and Ubuntu to install and use command line programs for Git, web, and digital projects. It takes about a half hour to set up. This is not at all required for the course; you can definitely use Git on Windows without Linux!

Visit https://git-scm.com/download and download the latest version for your operating system.

In the page Select Components you can leave the options at their defaults, but I recommend the following options:

  • Additional Icons > In the Quick Launch
  • Windows Explorer integration > Git Bash Here

Select Components

The Windows Explorer integration option allows opening the Git command prompt (Git Bash) from any folder from the Windows File Explorer by right-clicking on the folder and selecting Git Bash Here.

Choosing the default editor used by Git: find your text editor from the drop-down list (e.g. Visual Studio Code)

Default Text Editor

On the next screen (Adjusting your path environment), choose “Git from the command line and also from 3rd-party software”. This will make git commands available in Git Bash and Command prompt (FYI -- this course assumes you will use Git Bash with Windows).

Adjusting the name of the initial branch in new repositories: select Override the default branch name for new repositories and specify main as the default branch. This setting makes your Git configuration consistent with GitHub's default branch names.

git-default-branch

Configuring the line ending conversions: Checkout as-is, commit Unix-style line endings

Configuring Line Endings

Accept the rest of the default configurations. If you're on Windows, make sure that the box for Launch Git Bash is checked at the end of the installation. You will want to pin this program to your Taskbar or Start Menu for quick access to it in the future.

Launch Git Bash

Terminal

Computer terminals are interfaces that give you direct access to your computer's files, folders, and software. Git provides a terminal as part of the Windows installation, called Git Bash, and I highly recommend it as the terminal for this course. MacOS and Linux users can use the default Terminal applications because they work well with Git already.

Git Configuration

Let's set some default configrations for Git. Open your terminal and run the following commands to make sure Git is configured properly.

Does Git know who you are?

After running this command, the terminal should return your name.

git config --global user.name

If the terminal prompt returns nothing, run this command with your name: git config --global user.name "Firstname Lastname"

Does Git know your email address?

After running this command, the terminal should return your email address.

git config --global user.email

If the terminal prompt returns nothing, run this command with your email address:

git config --global user.email "[email protected]"

Make sure that the default branch is set to main

By default, git repositories start with a branch called master. This is based on the "master/slave" concept in computing. GitHub has committed to renaming its default branch from master to main for new repositories (read GitHub's statement on this change); however, most Git repositories uploaded to GitHub prior to 2020 still use master as the default / main branch. To align ourselves with this direction in practice, the init.defaultBranch configuration enables us to set main as the default branch.

$ git config --global init.defaultBranch main

Your First Repo

Open your terminal (Git Bash on Windows; Terminal on macOS/Linux). Opening your terminal will place you in your user's "Home" directory by default. When you log into your computer, you log in as a User. Each User has their own "Home" directory on your computer. If you type ls and press Enter on your keyboard, you should see a list of files and folders, including "Desktop" and "Documents". If you don't see these options, type cd and press Enter to run the command. This will bring you to your user's "Home" directory.

For this course, you might want to store all of your projects in a dedicated folder. Follow one of these options for creating your course project folder called learn-git on your computer:

1. Store learn-git folder within "Documents" folder:

cd Documents
mkdir learn-git
cd learn-git

2. Store learn-git folder on computer Desktop:

cd Desktop
mkdir learn-git
cd learn-git

The cd command means "Change Directory"; this is how you can navigate between folders on your computer from the command line. The mkdir command will "Make a Directory" as a subfolder of whichever folder you are currently in. If you ever lose track of where you are in terminal, the pwd command will return the full path of your current directory.

Now let's make your first git repository. You should now be in the learn-git directory (or whatever directory you want to be in to complete this tutorial). Make a new directory called first-repo and move into that directory:

mkdir first-repo
cd first-repo

Now, turn this folder into a git repository:

git init

Git is now active in your first-repo folder and will be tracking files from here on. Let's create a file and commit it to our repository. Run the following three commands in your terminal.

touch file.md
git add .
git commit -m "First commit"

The touch command will create a file in your current directory. The command expects you to specify the filename and exension (e.g. file.md). The git add . will add every untracked or modified file in your repo to the staging area. The git commit command will commit everything in the staging area to the git history, leaving a message (-m) that says "First commit".

To check your working directory, run the git status command. This will let you know if there are any untracked or modified files that need to be committed to the repo. If not, git will tell you nothing to commit, working tree clean.

Connecting Your Git Installation to Your GitHub Account

Working with Git almost always involves working with a remote Git hosting service, like GitHub. In order to push and pull between Git repositories on our machines and on our accounts in GitHub, we'll need to authenticate our credentials. We can do this once with Secure Shell (SSH), saving us the hassle of repeatedly logging in every time we push to a remote repository.

In your terminal, run: ssh-keygen -t rsa -C "[email protected]" (with the same email address you used to register your GitHub account).

This will generate a secure key on your machine that you will add to your GitHub account, allowing GitHub to identify your machine whenever you make a request to connect to one of your GitHub repositories via SSH. Here's how to respond to the prompts that follow:

  • Enter file in which to save the key: Press ENTER
  • Enter passphrase: Press ENTER for no passphrase
  • Enter same passphrase again: Press ENTER again

Next, we'll need to copy the contents of your public ssh key, which is a random string of letters and numbers, and save that to our GitHub account. To get the contents quickly, you can run this command if you've previously installed VS Code as your text editor: code ~/.ssh/id_rsa.pub

If you're not using VS Code, or if this did not work, you can find your public ssh key within a folder called .ssh in your computer's user's home directory. The public key is in a file called id_rsa.pub. Here's the directory path to the .ssh folder (where "USERNAME" is the username you use to login to your computer):

  • Windows: C:\Users\USERNAME\.ssh
  • macOS / Linux: /home/USERNAME/.ssh

You can open the id_rsa.pub file in any text editor (e.g. Notepad, TextEdit, VS Code, etc.) to copy the contents to your clipboard (Control + C).

In GitHub, go to Settings > SSH and GPG Keys and click New SSH Key. Enter a title to identify your computer. , Copy the SSH key from VS Code (Ctrl + a & Ctrl + c) and paste (Ctrl + v) the contents into the the Key field on GitHub. Be sure to copy and paste all the contents of the id_rsa.pub file, beginning at "ssh-ras ..." up to your email (including it). Click Add SSH key.

Verify that this worked by running this command in your terminal: ssh -T [email protected]

Type yes and press Enter at the prompt. If you see Hi user! You've successfully authenticated, but GitHub does not provide shell access. then you're all set!

These are the official instructions will walk you through the process of generating SSH keys and adding them to your GitHub account.

Push your First Repository to GitHub

Create a repository on GitHub. In the top-right of the screen, click on the + drop-down menu and select New Repository. Fill out the form for the new repository with first-repo as the Repository name. This repo can be public or private (your choice). Click on Create repository. This will create an empty repository on your GitHub account. The web address will be: https://github.com/YOUR_USERNAME/first-repo

You should see several options to add files to your repository. The default options use the HTTPS protocol, but we're going to use the SSH protocol since we set up our SSH keys already. Run these commands in your terminal (make sure you're in the first-repo directory we created above):

Two things: make sure you're in the first-repo directory we created above and swap in your GitHub username for "YOUR_USERNAME" in the git remote command.

git remote add origin [email protected]:YOUR_USERNAME/first-repo.git
git branch -M main
git push -u origin main

This should prompt a terminal response that looks like this:

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 217 bytes | 217.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/YOUR_USERNAME/first-repo.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

If so, refresh the repository page on GitHub and your file will be there!

Recommended for Windows: Set Git Bash as Default Terminal in VS Code

This part is optional, but will be useful if you plan on working with code or plain text in the future.

For this course, I recommend using VS Code as your text editor because it has an integrated terminal that makes it easy to run commands while working on project files. By default, VS code will use which ever default terminal you have set on your machine. In macOS it is Terminal (Linux also has a default terminal application called Terminal, but this might depend on your particular Linux distribution).

Windows users will need to add Git Bash as the integrated terminal in VS Code in order to use it within VS Code. To do this, you'll need to add the path to the git-cmd.exe executable file to the settings.json file in VS Code.

Finding Git on your Windows Computer

Git for Windows will automatically install the Git software files somewhere on your computer. We'll need to find where Git was installed in order to set the git-cmd.exe file as VS Code's integrated terminal. You'll have to look around your computer's C: drive for a folder called \Git\ that contains the git-cmd.exe executable. Here are some likely locations for it:

  • C:\Users\YOUR_USER\AppData\Local\Programs\Git\git-cmd.exe
  • C:\Program Files\Git\git-cmd.exe

Editing VS Code Settings

Go to the menu bar and click on File > Preferences to open the Settings menu. In the sidebar, click on Features > Terminal and scroll down to the Integrated › Shell: Windows setting. Click on the Edit in settings.json. This will open the .json file in VS Code.

The default setting has a line in the file that looks like this: "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",

Replace that line with this line block:

"terminal.integrated.shell.windows": "C:\\Users\\YOUR_USER\\AppData\\Local\\Programs\\Git\\git-cmd.exe",
    "terminal.integrated.shellArgs.windows": [
    "--command=usr/bin/bash.exe",
    "-l",
    "-i"
    ],

Note: that the path includes double \\ because the single \ need to be escaped in order to work.

From now on, to access your the Terminal with VS Code, you have a few options:

  • Use the Ctrl+` keyboard shortcut with the backtick character.
  • Use the View > Terminal menu option.
  • From the Command Palette (Ctrl+Shift+P), use the View: Toggle Integrated Terminal command

Read more information on VS Code's integrated Terminal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment