Getting Started with Git and GitHub

drawing

Section 1: Version Control with Git

Let’s create a new project directory and see how Git can help us track our changes.

note: The code command I use below only works if you’ve set Visual Studio Code as your Git editor. To do so, follow Bonus: Set Up Visual Studio Code instructions at the bottom of this page. Otherwise, you can skip lines starting with code, replacing them by creating and editing your readme.md file in another text editor.

mkdir example-repo # create a new folder
cd example-repo
git init # create a Git repository
git status
code readme.md # create and make changes to the readme in VSCode
git add readme.md # stages file
git commit -m"starting a readme for docs" # create a new commit with a log message
git log # to see our repos commit history. each commit has a unique ID, called a SHA
git show SHA # use SHA to see only its changes
code readme.md # make new changes to the readme
git diff # see only new differences
git add readme.md # add these changes
git reset # oops! didn't mean to add those

Now, you have a Git repo!

Control what you track with a Gitignore File

We want to track:

We do not want to track:

To avoid accidentally adding these files, we can use a gitignore file.

note: To use the code command used below, follow the Bonus: Set Up Visual Studio Code at bottom of this page. Otherwise, you can skip lines starting with code, instead creating and editing your data.csv, words.docx, words2.docx files in another text editor.

code data.csv # add a data file to the repository. We dont want to track this!
code words.docx # create a word document. We dont want to track this either
git status
code . # VSCode bolds all file names
code .gitignore # create a .gitignore file, add *.csv, *.docx, any other file extensions you dont want to track
git status
git add .gitignore
git commit -m"adding a gitignore file to ignore csv and docx files"
code words2.docx # create another word file
git status # git automatically ignores this new docx file
code . # ignored files are now lighter in VSCode

Version Control with Git: Summary

Bonus: Set Up Visual Studio Code

To use the code command and set Visual Studio Code as your Git editor, follow these instructions (from the VSCode docs).

Step 1: Download Visual Studio Code here.
Step 2: Add code command to path.

Step 3: In the terminal, type:

git config --global core.editor "code --wait"   

You’re all set!


Section 2: GitHub, Merging, and Branching

Section 3: Setting up Git and GitHub, Useful Links