← Back to Notes

Git Cheat Sheet

A quick reference for the most common Git commands — from setup and staging to pushing and branching.

Setup

Run these once when setting up Git on a new machine:

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

Verify your configuration:

git config --list

Starting a Repository

Initialize a new repo in an existing folder:

cd path/to/your/folder
git init

Or clone an existing repo from GitHub:

git clone https://github.com/username/repo.git

Checking Status

git status    # see what has changed in your working directory
git log       # view the full commit history

Staging and Committing

git add filename    # stage a specific file
git add .           # stage all changes at once
git commit -m "Your commit message here"

A commit is a permanent snapshot of everything you staged. Write commit messages in the present tense and make them descriptive — "add SSH key guide" not "stuff".


Working with Remote Repos

git remote add origin https://github.com/username/repo.git
git push -u origin main    # push commits to GitHub
git pull origin main       # fetch and merge remote changes
N
NOTE

Use master instead of main if your branch is named master. New repos default to main — check with git branch if unsure.


Everyday Workflow

git pull                        # 1. get the latest changes
# make your edits
git add .                       # 2. stage everything
git commit -m "describe change" # 3. commit
git push                        # 4. push to GitHub

Useful Commands

git diff              # see what changed before staging
git checkout file     # undo changes to a file
git branch            # list all branches
git branch newbranch  # create a new branch
git checkout newbranch # switch to a branch

Git Workflow at a Glance

StepCommandWhat happens
1git add .Files move from working directory to staging area
2git commit -m "message"Staging area saved as a snapshot in local repo
3git pushLocal commits uploaded to GitHub
4git pullRemote changes downloaded to working directory

Quick Reference

CommandWhat it does
git initInitialize a new Git repo
git clone <url>Download an existing repo
git statusShow staged, unstaged, and untracked changes
git add .Stage all changes
git commit -m "msg"Save a snapshot with a message
git pushUpload commits to remote
git pullDownload and merge remote changes
git logView commit history
git diffSee unstaged changes
git branchList branches
git checkout <branch>Switch branches