Version Control Tutorial: Basics

Working with Git Bash, GitHub, and making file changes

What is Version Control?

Version control is a way of recording changes to a file or set of files over time so that you can recall specific versions later.

The most common VC systems are:

  • Git

  • Mercurial
  • Subversion (SVN)

Why should I use Git and GitHub?

  • Peace of Mind - work is stored safely and can be easily recalled
  • Collaboration - multiple versions can be developed in tandem, allowing parallel feature development
  • Review and Discussion - GitHub allows for easy review of code changes before incorporating into the main branch of the repository
  • Documentation - shows when, why, and by whom specific changes were made
  • Consolidation - Project code and documentation can be stored in the same location.
  • Preservation and Access - Easy web browser access to all code versions, can control whether public or private.

Git versus GitHub

Git Logo

Git is a popular version control system, which is software we can download and use on our computer.

GitHub Logo

GitHub is a website that hosts Git repositories, and provides convenient ways to host documentation, discuss code changes, and report bugs.

The basic Git workflow

Minimalist Git use in your coding workflow involves only a few concepts, all of which we’ll cover today:

  1. Repository - A folder for storing code, with a special subdirectory containing the Git magic
  2. Staging - Reviewing your changes and selecting which ones to “add
  3. Local Copy - A copy (clone) of the repository on your computer where you “commit” your changes
  4. Remote Copy - The copy of the repo on a server (GitHub) where you “push” your changes to share them with others

The basic Git workflow

In practice

  • The repository accumulates changes incrementally as you build upon the project.
  • When you commit changes, Git tracks insertions and deletions instead of saving multiple copies, minimizing memory use.
  • Git also makes it easy to checkout past versions of code to diagnose issues or to branch multiple versions to develop in parallel.

In practice

  • The repository accumulates changes incrementally as you build upon the project.
  • When you commit changes, Git tracks insertions and deletions instead of saving multiple copies, minimizing memory use.
  • Git also makes it easy to checkout past versions of code to diagnose issues or to branch multiple versions to develop in parallel.

Git Bash

There are many ways of using the Git software, but we will focus on Git Bash, a command line Git interface that comes with Git.

First, open Git Bash. (Hit the Windows key and type “bash”)

Git Bash

There are many ways of using the Git software, but we will focus on Git Bash, a command line Git interface that comes with Git.

First, open Git Bash. (Hit the windows key and type “bash”)

Let’s start by asking Git what version we are running:

$ git --version

Git Bash

There are many ways of using the Git software, but we will focus on Git Bash, a command line Git interface that comes with Git.

First, open Git Bash. (Hit the windows key and type “bash”)

Let’s start by asking Git what version we are running:

$ git --version
git version 2.47.0.windows.2

Introduce Yourself to Git

  1. First, check if Git is already configured:
$ git config --global --list

If you see existing username and email settings, you can skip the next step.

Introduce Yourself to Git

  1. First, check if Git is already configured:

  2. Configure using your username and email (if needed):

$ git config --global user.name 'jmwingenroth'
$ git config --global user.email 'my_github_account_email@aol.com'

Introduce Yourself to Git

  1. First, check if Git is already configured:

  2. Configure using your username and email (if needed):

  3. Verify your configuration:

$ git config --global --list
user.name=jmwingenroth
user.email=my_github_account_email@aol.com

Git will now include your name and email when you publish changes!

Creating a Repository in GitHub

  1. Navigate to github.com, log in, and click the plus button in the top right corner

Creating a Repository

Creating a Repository in GitHub

  1. Navigate to github.com, log in, and click the plus button in the top right corner

  2. Select the repository owner, yourself!

The owner’s name will appear in the URL of the repository:

github.com/jmwingenroth/repository-name

Creating a Repository in GitHub

  1. Navigate to github.com, log in, and click the plus button in the top right corner

  2. Select the repository owner, yourself!

  3. Choose a repository name.

We recommend choosing something concise and descriptive, all lowercase, with dashes separating words, like my-repo-name.

Creating a Repository in GitHub

  1. Navigate to github.com, log in, and click the plus button in the top right corner

  2. Select the repository owner, yourself!

  3. Choose a repository name.

  4. Choose whether the repository is to be public or private.

Generally, choose private for repositories that will contain sensitive information, and public if the project requires it to be public. For today, choose private.

Creating a Repository in GitHub

  1. Navigate to github.com, log in, and click the plus button in the top right corner

  2. Select the repository owner, yourself!

  3. Choose a repository name.

  4. Choose whether the repository is to be public or private.

  5. Check the box to add a README file.

We will use this to store documentation for the repository.

Creating a Repository in GitHub

  1. Navigate to github.com, log in, and click the plus button in the top right corner

  2. Select the repository owner, yourself!

  3. Choose a repository name.

  4. Choose whether the repository is to be public or private.

  5. Check the box to add a README file.

  6. Choose a .gitignore template from the dropdown menu.

Select the .gitignore template for the programming language you will be using. Any language works for today.

Creating a Repository in GitHub

  1. Navigate to github.com, log in, and click the plus button in the top right corner

  2. Select the repository owner, yourself!

  3. Choose a repository name.

  4. Choose whether the repository is to be public or private.

  5. Check the box to add a README file.

  6. Choose a .gitignore template from the dropdown menu.

  7. Click the “Create Repository” button!!

Cloning Your First Repository With Git

Cloning = making a copy of a repository on your computer

Cloning Your First Repository With Git

  1. Copy the repository URL to the clipboard.

From the repository home page, click the green button, then copy the URL to clipboard by clicking the logo with intersecting squares.

Cloning Your First Repository With Git

  1. Copy the repository URL to the clipboard.

From the repository home page, click the green button, then copy the URL to clipboard by clicking the logo with intersecting squares.

Cloning Your First Repository With Git

  1. Copy the repository URL to the clipboard.

  2. Create a folder called “repos”

Open File Explorer, navigate to the folder where you’d like to store your code, create a folder named repos, and copy the file path using the right-click menu.

Then, navigate there using the cd command in Git Bash:

cd C:/Users/Jordan/repos

Cloning Your First Repository With Git

  1. Copy the repository URL to the clipboard.

  2. Choose where to store the git repository and navigate there.

  3. Enter the git clone <repo-url> command in Git Bash.

In the Git bash window, type git clone then paste in the repository URL from your clipboard by right-clicking:

git clone https://github.com/jmwingenroth/my-first-repo

Exploring Your Cloned Repository

After cloning, navigate into your repository folder:

cd <repository-name>

Now let’s check the status of our repository:

$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

This tells us we’re on the main branch and everything is up to date!

Making Changes

Let’s make a change to the README.md file. Open it in a text editor and add some content:

# My First Repository

This is my first Git repository for learning version control!

Now let’s see what Git thinks about our changes:

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   README.md

Viewing Changes with git diff

To see the specific changes you made, use git diff:

$ git diff
diff --git a/README.md b/README.md
index e69de29..9f4d96d 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1,2 @@
 # My First Repository
+
+This is my first Git repository for learning version control!

The output shows lines added (in green with +) and lines removed (in red with -), as well as the unchanged title line that GitHub added automatically.

Staging Changes with git add

Git detected our changes! Now we need to stage them before committing:

$ git add README.md

Let’s check the status again:

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   README.md

Our changes are now staged and ready to commit!

Creating a Commit with git commit

Now let’s create a commit with a descriptive message:

$ git commit -m "Add description to README file"
[main a1b2c3d] Add description to README file
 1 file changed, 2 insertions(+)

Let’s check our status again:

$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

Creating a Commit with git commit

Sharing Changes with git push

Our local repository now has changes that aren’t on GitHub yet. Let’s push them:

$ git push

Sharing Changes with git push

Our local repository now has changes that aren’t on GitHub yet. Let’s push them:

$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 10 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 314 bytes | 314.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/jmwingenroth/my-first-repo.git
   9c99bfd..aa653de  main -> main

Sharing Changes with git push

Now let’s check our status one final time:

$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

Perfect! Our changes are now saved both locally and on GitHub. Open or refresh the repo page in your web browser to see the changes.

Recap

At this point you should have:

  • ✓ Created a repository on GitHub
  • ✓ Cloned it to your local machine
  • ✓ Made changes to files
  • ✓ Staged changes with git add
  • ✓ Committed changes with git commit
  • ✓ Pushed changes to GitHub with git push
  • ✓ Given yourself a pat on the back! 🎉

Next Steps

At the next workshop, we will focus on creating and managing branches and pull requests, which are key to collaborating with other team members on GitHub.

Questions?