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
| Command | What 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 --versionoutput 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.
| Command | What 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
| Command | What 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
| Command | What 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
| Command | What 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
| Command | What 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
| Command | What 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.
| Command | What 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, andgit reset --hardoverwrites 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, andgit rebaserewrite commits. On a branch others have already pulled, prefergit revert.git push --forceoverwrites the remote branch.--force-with-leaserefuses if the remote moved since you last fetched it, so it is the safer one.git branch -donly deletes a branch that is fully merged into its upstream, or intoHEADif it has no upstream.-Dis--delete --forceand skips that check.git stashwithout-uleaves untracked files behind.- A shallow clone (
--depth=1) has its history cut off at the given number of commits, sogit logstops there. git switch -goes back to the previous branch, the same waycd -goes back to the previous directory.
related on terminal-life
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.
- Reference index: https://git-scm.com/docs
- https://git-scm.com/docs/git-config
- https://git-scm.com/docs/git-init
- https://git-scm.com/docs/git-clone
- https://git-scm.com/docs/git-status
- https://git-scm.com/docs/git-add
- https://git-scm.com/docs/git-diff
- https://git-scm.com/docs/git-commit
- https://git-scm.com/docs/git-log
- https://git-scm.com/docs/git-show
- https://git-scm.com/docs/git-blame
- https://git-scm.com/docs/git-branch
- https://git-scm.com/docs/git-switch
- https://git-scm.com/docs/git-merge
- https://git-scm.com/docs/git-remote
- https://git-scm.com/docs/git-fetch
- https://git-scm.com/docs/git-pull
- https://git-scm.com/docs/git-push
- https://git-scm.com/docs/git-restore
- https://git-scm.com/docs/git-reset
- https://git-scm.com/docs/git-revert
- https://git-scm.com/docs/git-stash
- https://git-scm.com/docs/git-reflog
- https://git-scm.com/docs/git-rebase
terminal-life.com/cheatsheets/git/ · checked against the Git 2.56.0 documentation