Skip to main content

Git & GitHub

A practical reference for the Git commands and GitHub workflows you will use every day.

Git Basics

Git is version control software that tracks changes to your code over time. GitHub is a website that hosts your Git repositories so you can access them from anywhere and share them with others.

Every change you make to a project can be saved as a commit — a named snapshot with a message. If something breaks, you can look back through your commits, understand what changed, and undo it if needed.

Configure Git once

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Check the status of your work (run this constantly)

git status

Daily Workflow

The loop you repeat on every working session:

# 1. pull latest changes before you start
git pull

# 2. make your changes to files

# 3. check what changed
git status

# 4. stage the files you want to commit
git add filename.js
# or stage everything:
git add .

# 5. commit with a meaningful message
git commit -m "Add contact form validation"

# 6. push to github
git push

Always pull before you push. If someone else pushed while you were working, you need to bring in their changes first.

Branching

A branch lets you work on a feature or fix without touching the main codebase. When it is ready, you merge it back.

# create and switch to a new branch
git checkout -b feature/add-login

# see all branches
git branch

# switch to an existing branch
git checkout main

# merge a branch into main (from main)
git merge feature/add-login

# delete a branch after merging
git branch -d feature/add-login

Name branches clearly: feature/nav-links, fix/login-bug, refactor/cleanup-styles.

Pull Requests

A pull request (PR) is a GitHub feature for proposing changes from your branch into another branch. It is the standard way teams review code before merging.

Basic PR flow:

  1. Push your branch to GitHub
  2. Go to the repository on GitHub
  3. Click Compare & pull request
  4. Write a title and description explaining what you changed and why
  5. Request a review if applicable
  6. Merge when approved

Write a good PR description: What does it do? Why was the change needed? Is there anything reviewers should pay attention to?

Merge Conflicts

A merge conflict happens when two branches changed the same lines in a file and Git cannot figure out which version to keep. You have to resolve it manually.

What a conflict looks like in a file:

<<<<<<< HEAD
const greeting = 'Hello'
=======
const greeting = 'Hi there'
>>>>>>> feature/update-greeting

How to resolve it:

  1. Open the file
  2. Decide which version (or a combination) is correct
  3. Delete the conflict markers (<<<<<<<, =======, >>>>>>>)
  4. Save the file
  5. Stage and commit the resolved file
git add greeting.js
git commit -m "Resolve merge conflict in greeting"

Common Errors

ErrorWhat it usually meansWhat to do
Permission denied (publickey)SSH key not set up or not addedRe-check SSH setup
rejected - non-fast-forwardRemote has commits you don't haveRun git pull first, then push
nothing to commitNo changes stagedCheck git status — you may not have saved the file
detached HEADYou checked out a commit directlyRun git checkout main to get back
merge conflictTwo branches changed the same linesResolve the conflict manually (see above)

Cheat Sheet

# setup
git config --global user.name "Name"
git config --global user.email "email"

# start
git init                      # initialize a new repo
git clone REPO_URL            # copy a remote repo locally

# daily
git status                    # see what changed
git add filename.js           # stage a file
git add .                     # stage all changes
git commit -m "message"       # save a snapshot
git push                      # send commits to github
git pull                      # get latest from github

# branches
git branch                    # list branches
git checkout -b BRANCH_NAME   # create + switch to branch
git checkout BRANCH_NAME      # switch to existing branch
git merge BRANCH_NAME         # merge branch into current
git branch -d BRANCH_NAME     # delete a branch

# history
git log --oneline             # compact commit history
git diff                      # see unstaged changes
git diff --staged             # see staged changes