Cheat sheets git

git cheat sheet: setup, daily commits, branches, remotes, and undo

git tracks changes to a directory of files, so you can see what changed, go back to an earlier state, and share work through a remote. This sheet groups the commands by what you are trying to do: set up, record work, look at history, branch, sync, and undo. Every command and flag below is from the official Git documentation at git-scm.com, which describes Git 2.56.0, the latest release. We attempt to test on all versions.

what it is and when to use it

Use git for anything you would hate to lose or want to see the history of: code, configs, dotfiles, notes. It works fully offline. A remote such as a GitHub repository is optional and only matters when you push or pull.

  • Commit: a snapshot of the index, with a message, an author, and a parent commit.
  • Branch: a movable name that points at a commit.
  • HEAD: the commit (usually via a branch) you have checked out right now.
  • Remote: another copy of the repository, usually called origin.

install and check the version

CommandWhat it does
sudo apt-get install git
Debian or Ubuntu
sudo dnf install git
Fedora
sudo pacman -S git
Arch Linux
brew install git
macOS with Homebrew
git --version
output
git version 2.56.0
Print the installed version

first-time setup

Git puts your name and email on every commit, so set them once per machine. --global writes to your user config file (~/.gitconfig), not the repository.

CommandWhat it does
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Name and email that go on your commits
git config --global init.defaultBranch main
Name new repositories' first branch main
git config --list --show-origin
Show every setting and the file it came from

start a repository

CommandWhat it does
git init
Turn the current directory into a repository
git clone https://example.com/project.git
Copy a remote repository into a new directory
git clone --depth=1 https://example.com/project.git
Shallow clone: only the latest commit, not the full history

record work

CommandWhat it does
git status -s
What changed, in short form
git add notes.md
Stage one file
git add -p
Pick changes hunk by hunk
git diff
Unstaged changes
git diff --staged
Staged changes
git commit -m "Describe the change"
Commit what is staged
git commit -a -m "Describe the change"
Stage every tracked file that changed and commit it in one step
git commit --amend
Replace the last commit with the index, for example to add a forgotten file

git commit -a does not pick up new files. Those still need git add first.

read history

CommandWhat it does
git log --oneline --graph --all
One line per commit, with a branch graph, across every branch
git log -p -- notes.md
Every commit that touched one file, with its diff
git log -S "API_TIMEOUT"
Commits that added or removed a string
git show HEAD
One commit: message and diff
git blame -L 10,20 notes.md
Who last changed lines 10 to 20 of a file

The site’s git lg and git last aliases wrap the first and last of these into two short commands.

branches

CommandWhat it does
git branch
List local branches
git branch -a
List local and remote-tracking branches
git switch -c fix-typo
Create a branch and switch to it
git switch main
Switch to an existing branch
git switch -
Switch back to the previous branch
git merge fix-typo
Merge a branch into the one you are on
git branch -d fix-typo
Delete a branch that is merged
git branch -D fix-typo
Force-delete a branch that is not merged

remotes and sync

CommandWhat it does
git remote -v
List remotes with their URLs
git fetch --prune
Download new commits, and drop remote-tracking branches deleted on the remote
git pull
Fetch and integrate in one step
git push -u origin fix-typo
Push a new branch and make it track the remote one
git push --force-with-lease
Push after a rebase, but refuse if someone else pushed in the meantime

undo

Read the comment before you run each one. Some of these throw work away.

CommandWhat it does
git restore --staged notes.md
Unstage a file. The change stays in the working tree
git restore notes.md
Throw away unstaged changes to a file. They are gone
git reset --soft HEAD~1
Undo the last commit and keep its changes staged
git revert <commit>
Make a new commit that reverses an old one. Safe on shared branches
git stash push -u
git stash list
git stash pop
Put changes aside, including untracked files, then bring them back
git reflog
Every place HEAD has pointed recently, to find a "lost" commit

The site’s git undo and git stash-all aliases are the reset --soft and stash push lines above.

gotchas

  • git restore <file> overwrites the file with the staged version, and git reset --hard overwrites tracked files with the commit’s version. Uncommitted edits to those files are lost, so commit or stash first if you might want them.
  • git commit --amend, git reset, and git rebase rewrite commits. On a branch others have already pulled, prefer git revert.
  • git push --force overwrites the remote branch. --force-with-lease refuses if the remote moved since you last fetched it, so it is the safer one.
  • git branch -d only deletes a branch that is fully merged into its upstream, or into HEAD if it has no upstream. -D is --delete --force and skips that check.
  • git stash without -u leaves untracked files behind.
  • A shallow clone (--depth=1) has its history cut off at the given number of commits, so git log stops there.
  • git switch - goes back to the previous branch, the same way cd - goes back to the previous directory.

sources

Checked against the Git 2.56.0 reference documentation on git-scm.com. On an older Git, git help <command> shows the documentation for your version.