Free Git cheat sheet

Git cheat sheet

The Git commands you use every day on one page: stage, commit, branch, merge, push, pull, and how to undo without panic.

Start a repoStage and commitBranchesSync with a remoteUndo (carefully)Stash work in progress

← All cheat sheets

Git Cheat Sheet

Quick reference from FreeCodingCourses.com

Start a repo

git init
turn the current folder into a repo.
git clone <url>
copy a remote repo to your machine.
git status
what changed, what is staged. Run it constantly.
git config --global user.name 'You'
set your name once per machine.

Stage and commit

git add file.txt
stage one file for the next commit.
git add .
stage everything changed in this folder down.
git commit -m "message"
save the staged snapshot with a message.
git commit -am "message"
stage tracked files and commit in one step.
git log --oneline
compact history, one commit per line.
git diff
see unstaged changes; add --staged for staged.

Branches

git branch
list branches; the current one has a *.
git switch -c feature
create and move to a new branch.
git switch main
move to an existing branch.
git merge feature
merge feature into the branch you are on.
git branch -d feature
delete a merged branch.

Sync with a remote

git remote -v
show the remotes you are connected to.
git push
send your commits to the remote.
git push -u origin feature
push a new branch and track it.
git pull
fetch and merge the remote's new commits.
git fetch
download remote changes without merging yet.

Undo (carefully)

git restore file.txt
throw away unstaged changes to a file.
git restore --staged file.txt
unstage, keep the edits.
git commit --amend
fix the last commit's message or contents.
git revert <hash>
make a new commit that undoes an old one. Safe.
git reset --soft HEAD~1
undo the last commit, keep the changes staged.

Stash work in progress

git stash
shelve uncommitted changes and get a clean tree.
git stash pop
bring the stashed changes back.
git stash list
see what you have shelved.

Unlock the full Git cheat sheet

You're seeing 2 of 6 sections. Drop your email to open the rest plus the gotchas, and save the whole thing as a printable PDF.

No spam, unsubscribe anytime. See our privacy policy.

Git questions, answered

What is the difference between Git and GitHub?

Git is the version-control tool that runs on your computer and tracks changes. GitHub is a website that hosts Git repositories online so you can back them up and collaborate. You can use Git with no GitHub account at all; GitHub just adds a shared remote.

I made a mistake. What is the safest way to undo?

If the change is not committed, git restore drops it. If you committed but have not shared it, git commit --amend or git reset --soft HEAD~1 rework it. If the commit is already pushed, use git revert, which adds a new commit that cancels the old one without rewriting shared history.

git switch or git checkout?

They overlap, but git switch (and git restore) are the newer, clearer commands: switch changes branches, restore changes files. git checkout does both jobs and more, which is why it confused people for years. Use switch and restore in new habits.

Where to go next