Git Cheatsheet

View saved

Git tracks versions of your project as commits. This sheet lists the commands you use most often so you can look up the right spelling and flags without rereading a whole lesson.

Prefer small commits with clear messages. When something goes wrong, start with the safe undo options—check status and history before rewriting shared work.

Setup

git --version

Confirm Git is installed and see which version you have.

git --version

git config --global user.name

Set the name attached to your commits on this machine.

git config --global user.name "Alex Rivera"

git config --global user.email

Set the email Git stores with each commit (use the address tied to GitHub if you push there).

git config --global user.email "[email protected]"

git init

Turn the current folder into a new Git repository (creates a hidden .git directory).

cd my-project
git init

git clone

Copy a remote repository onto your computer, including history and a link back to the remote.

git clone https://github.com/org/repo.git
cd repo

Everyday commits

git status

Show which files are modified, staged, or untracked. Run this often.

git status

git add

Stage changes so they will be included in the next commit. Use a path or . for everything in the folder.

git add README.md
git add .

git commit -m

Save a snapshot of staged changes with a short message. Write messages in the imperative mood.

git commit -m "Add contact form validation"

git diff

Show unstaged line changes. Add --staged to see what is already staged.

git diff
git diff --staged

git restore

Discard unstaged edits in a file (destructive for those edits). Prefer this over older checkout -- wording.

git restore notes.txt

.gitignore

List patterns Git should ignore (secrets, build output, OS junk). Commit the ignore file itself.

# .gitignore
.env
__pycache__/
*.log
node_modules/

History

git log

List commits from newest to oldest. Use one-line and graph options for a faster scan.

git log --oneline --graph --decorate -n 15

git show

Inspect one commit: message plus the patch it introduced.

git show HEAD
git show a1b2c3d

git blame

Annotate each line of a file with the last commit that touched it—useful for “who changed this?”

git blame src/app.py

git log -p

Walk history and print the full diff of each commit. Narrow with a path.

git log -p -- README.md

git shortlog -sn

Summarize commit counts by author (handy for a quick activity overview).

git shortlog -sn

Branches

git branch

List local branches. The star marks the branch you are on.

git branch

git switch -c

Create a new branch and switch to it in one step (modern alternative to checkout -b).

git switch -c feature/login-page

git switch

Move your working tree to another existing branch.

git switch main

git merge

Bring another branch’s commits into the branch you are currently on.

git switch main
git merge feature/login-page

Conflict markers

After a conflicted merge, edit the file, remove <<< / === / >>> markers, then stage and commit.

git add conflicted-file.txt
git commit -m "Merge feature/login-page; resolve form title"

Remotes & GitHub

git remote -v

List remotes and their fetch/push URLs. origin is the usual name for the main remote.

git remote -v

git push -u

Upload your branch and set upstream tracking so later you can run plain git push / git pull.

git push -u origin feature/login-page

git pull

Fetch from the remote and integrate into your current branch (typically fetch + merge).

git pull origin main

git fetch

Download remote updates without merging them yet—safer when you want to inspect first.

git fetch origin
git log HEAD..origin/main --oneline

Pull request

On GitHub, open a PR to propose merging your branch into main after review—not a Git binary command, but the usual team workflow.

# After pushing your branch:
# GitHub → Pull requests → New pull request

git clone --depth 1

Shallow clone with only recent history—faster for huge repos when you do not need full history.

git clone --depth 1 https://github.com/org/big-repo.git

Undo safely

git restore --staged

Unstage a file but keep your edits in the working tree. Safe default when you staged too much.

git restore --staged secrets.env

git commit --amend

Fold new staged changes into the latest commit or fix its message. Only amend commits you have not pushed (or that only you use).

git add typo-fix.txt
git commit --amend --no-edit

git revert

Create a new commit that undoes an earlier one. Preferred on shared branches because history stays intact.

git revert a1b2c3d

git reset --soft HEAD~1

Move HEAD back one commit but keep changes staged. Useful right after a mistaken local commit.

git reset --soft HEAD~1

git reset --hard

Throw away commits and matching working-tree changes. Destructive—never use on commits others already pulled unless the team agrees.

git reset --hard HEAD~1

Comments

One comment per signed-in account. Comments are saved with this page’s URL.