PLAINFINCH learn by building

Git cheat sheet

The 74 commands you will use most, on one page. The everyday ones come first. Print it, or keep it open beside your editor.

Full reference

Everyday 39

.gitignore
A file listing patterns for files and folders Git should leave untracked.
branch
A movable label for a line of work, so changes can grow apart from main.
commit
A saved snapshot of your project with a message, an author and a unique id.
conflict markers
The <<<<<<<, ======= and >>>>>>> lines Git adds to show both sides of a conflict.
git add .
Stage every new and changed file in this folder and the folders below it.
git add <file>
Put a file's current changes into the staging area, ready for the next commit.
git branch
List your local branches and mark the one you are on.
git clone
Download a full copy of a remote repository, history and all, into a new folder.
git commit -m
Save everything in the staging area as a new commit with the message you give.
git config user.email
Set the email address that Git records alongside your commits.
git config user.name
Set the name that Git writes into each commit you make.
git diff
Show the lines you have changed in your files but not staged yet.
git diff --staged
Show the changes that are staged and will go into the next commit.
git fetch
Download new commits from the remote without changing your own branches.
git init
Turn the current folder into a new repository so Git can start tracking it.
git log
List the commits on this branch, newest first, with author, date and message.
git log --oneline
Show each commit on one line with its short id and summary.
git merge
Bring the commits from another branch into the branch you are on.
git pull
Fetch new commits from the remote and merge them into your current branch.
git push
Send your new commits on this branch up to the remote.
git push -u origin <branch>
Push a branch for the first time and remember the remote branch it tracks.
git remote -v
List the remotes this repository knows about and their addresses.
git remote add
Connect your repository to a remote, usually called origin, at a given URL.
git restore --staged
Take a file out of the staging area while keeping your edits in the file.
git restore <file>
Throw away unstaged edits to a file and bring back its last saved version.
git revert
Make a new commit that undoes an earlier one, leaving history intact.
git status
Show which branch you are on and which files are new, changed or staged.
git switch
Move to another branch so your files match the commits on it.
git switch -c
Create a new branch and move onto it in one command.
GitHub
A website that hosts Git repositories online so you can share and back them up.
HEAD
A pointer to the commit you are on now, usually the tip of your current branch.
main
The usual name for a repository's primary branch.
merge conflict
What happens when two branches change the same lines and Git needs you to choose.
origin
The name Git gives by default to the remote you cloned from or first added.
pull request
A request on GitHub to review a branch's changes and merge them into another branch.
remote
Another copy of your repository, usually on GitHub, that you push to and pull from.
repository
A project folder plus the full history of saved versions Git keeps for it.
staging area
The waiting room where you collect changes before saving them as a commit.
working folder
The files you see and edit, which may differ from the last commit.

Also useful 35

.env file
A file that holds private keys and settings, which should never be committed.
ahead and behind
How many commits your branch has that the remote lacks, and the other way round.
fast-forward
A merge where Git simply moves the branch label ahead because nothing diverged.
git add -A
Stage all changes in the whole repository, including deleted files.
git branch -a
List local branches along with the remote-tracking ones such as origin/main.
git branch -d
Delete a branch whose work has already been merged somewhere safe.
git branch <name>
Create a new branch at the current commit without switching to it.
git checkout -b
The older way to create a branch and switch to it at the same time.
git checkout <branch>
The older command for moving to another branch, still widely used.
git clean -n
Preview which untracked files git clean would delete, without deleting anything.
git commit --amend
Replace your last commit with a corrected one that includes what is staged now.
git commit --amend -m
Give your most recent commit a new message without making a new commit.
git commit -am
Stage changes to tracked files and commit them in one step.
git diff <a> <b>
Compare two commits or branches and show every line that differs between them.
git log --all
Include commits from every branch in the log, not just the one you are on.
git log -n
Limit the log to the most recent few commits, for example git log -n 3.
git merge --abort
Cancel a merge that hit conflicts and go back to how things were before.
git reflog
List every place HEAD has pointed recently, so you can find lost commits.
git reset --hard
Move the branch back and erase uncommitted changes, which cannot be undone easily.
git reset --mixed
Move the branch back and keep the changes in your files, but unstaged.
git reset --soft
Move the branch back to an earlier commit and keep those changes staged.
git restore .
Discard all unstaged edits in this folder at once, so use it with care.
git rm
Delete a tracked file and stage that deletion for the next commit.
git rm --cached
Stop tracking a file but leave it in your folder, often after adding it to .gitignore.
git show
Display one commit's message along with the exact changes it made.
git stash
Set uncommitted changes aside so you have a clean working folder for a while.
git stash pop
Bring back your most recently stashed changes and remove them from the stash.
git status -s
Print a short status with one line per file and a two-letter code.
GitHub Pages
A free GitHub service that publishes a repository's files as a website.
HEAD~1
The commit just before HEAD; HEAD~2 goes back two, and so on.
merge commit
A commit with two parents that joins the histories of two branches.
origin/main
Your local record of where main was on the remote the last time you fetched.
rejected push
Git refusing your push because the remote has commits you have not pulled yet.
untracked file
A file in your folder that Git has not been told to follow yet.
upstream branch
The remote branch your local branch is linked to for plain push and pull.

PlainFinch Git cheat sheet · plainfinch.com/git/cheat-sheet/ · version 3.25.2