Git and GitHub
In-Depth Guides
- Git Workflow Recommendation
- Git Branches
- Git Remote v Local
- Git Merge Conflicts
- Git Rebasing
- Git Undoing Changes & Debugging
- Git Pull Requests
Introduction
Git is a distributed version control system that enables developers to track changes in their code over time. Unlike centralized version control systems, Git gives each developer a complete copy of the repository, making operations like branching and merging faster and more flexible. This guide covers fundamental Git concepts, commands, and workflows to help you effectively manage your code.
Git and GitHub are tools we will be using throughout the course to track and store changes to our code and allow you to collaborate with others and keep everything in sync.
Note: You do not need GitHub to use Git, but you cannot use GitHub without using Git.
🎥 Video: Git & GitHub Crash Course For Beginners (32 mins) - A comprehensive introduction to Git and GitHub
What is Version Control?
Version control is a system that records changes to files over time, allowing you to recall specific versions later. It gives developers the ability to:
- Track code changes
- Review the history of code changes
- Recover previous versions of files
- Collaborate with other developers
Git is currently the most widely used version control system in software development due to its speed, distributed nature, and robust branching capabilities.

🎥 Video Short: What exactly is Git?! (1 min)
Why is Git Important?
Git addresses several critical needs in modern software development:
- Collaboration: Multiple developers can work on the same project without overwriting each other’s changes
- History: Every change is tracked with information about who made it and why
- Branching: Teams can work on different features simultaneously in isolated environments
- Backup: Every developer has a complete copy of the project, providing redundancy
- Experimentation: Developers can try new ideas without affecting the main codebase
Below is an example of checking your Git history. This shows 2 most recent commits. One added a user authentication feature and the other fixed a navigation menu bug on mobile devices. We cover more about history/branching in the Git Branches guide.
$ git log
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
Author: John Doe <john.doe@example.com>
Date: Mon Sep 11 14:23:44 2023 -0700
Add user authentication feature
commit b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1
Author: Jane Smith <jane.smith@example.com>
Date: Sun Sep 10 11:05:32 2023 -0700
Fix navigation menu bug on mobile devices
đź’ˇ Tip: Git is valuable even for solo projects. It provides a safety net for experimental changes and allows you to document your development journey.
🎥 Video: Why Use Git? (6 mins) - The benefits of version control for teams and individuals
What is GitHub?
GitHub is a code hosting platform for version control and collaboration. While Git is the version control system that runs locally on your computer, GitHub is a remote service that hosts your Git repositories online.
How GitHub Relates to Git
- Git is the version control tool that tracks changes locally
- GitHub is a platform that:
- Hosts Git repositories
- Facilitates collaboration through features like Pull Requests and Issues
- Provides web-based visualization of repository history and changes
- Adds social features like following users and starring repositories
- Offers additional development tools like GitHub Actions for CI/CD

⚠️ Note: While GitHub is the most popular Git hosting service, alternatives include GitLab, Bitbucket, and Azure DevOps.
🎥 Video: Git vs. GitHub: What’s the difference? (10 mins) - Understanding the relationship between Git and GitHub
Key Git Vocabulary
Repository (Repo)
A repository is a collection of files and the history of changes made to those files. It contains the entire project and its version history.
# Create a new repository
$ git init my-project
Initialized empty Git repository in /path/to/my-project/.git/
# Clone an existing repository
$ git clone https://github.com/username/repository.git
Cloning into 'repository'...
remote: Enumerating objects: 125, done.
remote: Counting objects: 100% (125/125), done.
remote: Compressing objects: 100% (80/80), done.
remote: Total 125 (delta 58), reused 95 (delta 40)
Receiving objects: 100% (125/125), 2.02 MiB | 3.45 MiB/s, done.
Resolving deltas: 100% (58/58), done.
Commits
A commit is a snapshot of your repository at a specific point in time. Each commit has:
- A unique identifier (hash)
- A commit message describing the changes
- Information about who made the commit and when
- A reference to the previous commit(s)
# Stage changes for commit
$ git add file.txt
# Create a commit with a message
$ git commit -m "Add user authentication feature"
[main a1b2c3d] Add user authentication feature
1 file changed, 25 insertions(+), 2 deletions(-)
# View commit history
$ git log --oneline
a1b2c3d Add user authentication feature
b2c3d4e Fix navigation menu bug on mobile devices
c3d4e5f Initial commit
Best practices for commits:
- Make atomic commits (each commit should represent a single logical change)
- Write clear, descriptive commit messages
- Commit regularly to avoid large, unwieldy changes

🎥 Video: How to commit better with Git (12 mins) - How to write effective commit messages
Branches
A branch is a parallel version of the repository that allows you to work on different features or fixes without affecting the main codebase.
# Create a new branch
$ git branch feature/login
# Switch to a branch
$ git checkout feature/login
Switched to branch 'feature/login'
# Create and switch to a new branch (shorthand)
$ git checkout -b feature/payment
Switched to a new branch 'feature/payment'
# List all branches (* indicates current branch)
$ git branch
main
feature/login
* feature/payment
Common branching strategies:
- Feature branching: Create a branch for each new feature
- Release branching: Create branches for preparing releases
- Gitflow: A specific branching model with develop, feature, release, and hotfix branches
This is a diagram of Gitflow.

🎥 Video: Git Branches Tutorial (33 mins) - How to work with branches effectively
Forks
A fork is a copy of someone else’s repository that lives in your GitHub account. Forking allows you to freely experiment with changes without affecting the original project. To create a fork, click the Fork button on the top right of a repository’s page on GitHub.
# After forking on GitHub, clone your fork locally
$ git clone https://github.com/your-username/forked-repository.git
# Add the original repository as a remote called "upstream"
$ git remote add upstream https://github.com/original-owner/original-repository.git
# Verify remotes
$ git remote -v
origin https://github.com/your-username/forked-repository.git (fetch)
origin https://github.com/your-username/forked-repository.git (push)
upstream https://github.com/original-owner/original-repository.git (fetch)
upstream https://github.com/original-owner/original-repository.git (push)
đź’ˇ definitions for above output:
remote repository: The repository that you forked from.
upstream: The original repository that you forked from. (not technically “yours”)
origin: Your forked repository.
Initial Setup
To get started, you will need to download, install and configure Git on your computer and create a free GitHub account here.
Install Git
- Download the latest version based on your operating system:
- Follow the instructions as provided in the Download until the installation is complete.
- Open the command line terminal
- Type
git versionto verify Git was installed.
Setting your user in Git
Git uses a username and email to associate commits with an identity. Let’s define your user with the following commands
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
Authenticating with Github from Git
When you connect to a Github repository from Git, you will need to authenticate using either HTTPs or SSH. We recommend connecting over HTTPS and caching your GitHub credentials in Git following these instructions
Working with Git & GitHub through the command line
The general development flow is:
- Make local code changes on your computer.
- Commit your local changes. This saves your progress locally.
- Push your new changes to GitHub. This stores your progress on the cloud.
- Refresh the repository by pulling in the latest code from GitHub. This ensures the code you are working on is the most up-to-date code and prevent conflicts when looking to push new changes into GitHub
💡 Tip: Commit often and commit early, this way you’ll never have that gut sinking feeling of overwriting or losing changes.
Below are some walkthroughs steps for:
- Setting up a local Git repository
- Pushing a new repository onto GitHub
- Pushing new changes when your local code is updated
- Pulling Changes from GitHub
Creating a local Git repository
Local Git repositories are created and managed locally on your computer.
To begin, open up a terminal and create a new directory, open it and perform git init to create a new repository
mkdir newproject
cd newproject/
git init
Adding and committing changes to your local repository
Now that your local Git repository is up and running, you can update it with any changes you make.
-
Check for any changes that have been made or new files since your last commit
git status -
Stage any files with changes (or new ones) you would like to commit
git add <path>: This stages an individual file or directorygit add .: This stages all files with changes
-
Check to see what was added
git status -
Commit the changes to your local repository with a brief message outlining the changes
git commit -m "your message about the commit"
Note: Git will notice any new files inside the repository but it won’t track the files unless you explicitly tell it to.
Create a new repository on GitHub
To put your project up on GitHub, you will need to create a repository for it. To create a new repository:
- Go to GitHub and Log in with your account.
- In the upper-right corner of any page, use the drop-down menu, and select New repository.
- Type a short, memorable name for your repository.
- Optionally, add a description of your repository.
- Choose a repository visibility.
- Click Create repository.
For more details, checkout GitHub documentation on Creating a new repository.
Linking the local repository with GitHub repository
Since we’ve already created our repository locally, we want to push that onto our newly created GitHub repository.
-
Connect your local repository to GitHub repository
git remote add origin https://github.com/yourUserName/yourRepoName.git -
Push your local changes to your remote repository
git push -u origin main -
Browse to your GitHub repository web page to refresh and see your code
Syncing the local repository with your remote repository
To update your local repository to the most recent changes that you or others have push to GitHub, execute git pull
Note:
git pulldoes two things behind the scene: fetch & merge remote changes. Git tries to auto-merge, but unfortunately this is not alway possible and may result in conflicts. You are responsible to merge those conflicts manually by editing the files shown by Git.
Cloning a remote repository
If you are collaborating on a project, or just want access to your project from a different computer, you can clone the project from the GitHub repository.
Cloning a repository is typically only done once, at the beginning of your interaction with the project. Once you have cloned a repository, you won’t need to clone it again to do regular development.
To clone a repository:
-
Find the GitHub HTTPS repository url on the main page of the repository.
-
Open the terminal
-
Change the current working directory to the location where you want to clone the repository
-
Type
git clone, an the paste the repository URL & press Entergit clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
Setting up a repository from starter code
Throughout the program, you may be provided initial code to get you started. If so, follow this steps to setup your own repository:
- Go to the starter code GitHub repository.
- Find and click the green Code button, and follow the instructions to download the project in a
.zipfile - Unzip the project on your computer’s desired location.
Once you have the starter code in your computer, let’s open up a terminal to initialize the git repository.
cd /path/to/unzipped/project
git init
git add .
git commit -m "initial starter code setup"