Git cheat sheet
96 one-line examples for Git. Open it while you code, or print it and keep it near the keyboard. Free, with no signup and no ads.
Setup and Config
| Code | What it does |
|---|---|
git --version | Show the installed Git version. |
git config --global user.name "Your Name" | Set the name on your commits. |
git config --global user.email "[email protected]" | Set the email on your commits. |
git config --list | Show all current config settings. |
git config --global core.editor nano | Set nano as the default editor. |
git config --global init.defaultBranch main | Make main the default branch name. |
git help commit | Open the manual page for a command. |
git config --global --unset user.name | Remove one config setting. |
git config --global color.ui auto | Turn on colored output. |
git config --global pull.rebase false | Use merge when you pull. |
git config --global alias.st status | Create a short name for a command. |
git config --global --edit | Open the config file in your editor. |
Start a Repository
| Code | What it does |
|---|---|
git init | Create a new repository in this folder. |
git init my-project | Create a new repository in a new folder. |
git clone https://host/repo.git | Copy a remote repository to your machine. |
git clone https://host/repo.git mydir | Copy a repository into a named folder. |
git status | Show changed, staged, and untracked files. |
git status -s | Show a short status list. |
git add file.txt | Stage one file for the next commit. |
git add . | Stage all changes in this folder. |
git add -A | Stage all changes in the repository. |
git rm file.txt | Remove a file and stage the removal. |
git mv old.txt new.txt | Rename a file and stage the change. |
git restore --staged file.txt | Unstage a file and keep its changes. |
Save Changes
| Code | What it does |
|---|---|
git commit -m "Add login page" | Save staged changes with a short message. |
git commit -am "Fix typo" | Stage tracked files and commit them. |
git commit --amend -m "New message" | Replace the last commit message. |
git commit --amend --no-edit | Add staged files to the last commit. |
git log | Show the commit history. |
git log --oneline | Show history as one line per commit. |
git log --oneline --graph | Show history as a simple graph. |
git log -p file.txt | Show changes to one file over time. |
git show HEAD | Show the last commit and its changes. |
git diff | Show unstaged changes. |
git diff --staged | Show staged changes. |
git diff HEAD~1 HEAD | Compare the last two commits. |
Branches
| Code | What it does |
|---|---|
git branch | List local branches. |
git branch -a | List local and remote branches. |
git branch feature-login | Create a new branch. |
git switch feature-login | Move to an existing branch. |
git switch -c feature-login | Create a branch and move to it. |
git switch - | Move back to the previous branch. |
git branch -d feature-login | Delete a merged branch. |
git branch -D feature-login | Force delete a branch. |
git branch -m new-name | Rename the current branch. |
git merge feature-login | Join another branch into the current one. |
git merge --abort | Stop a merge and return to the old state. |
git branch --merged | List branches already merged here. |
Remote Work
| Code | What it does |
|---|---|
git remote -v | List remote names and URLs. |
git remote add origin https://host/repo.git | Add a remote named origin. |
git remote remove origin | Remove a remote. |
git fetch origin | Download remote changes but do not merge. |
git pull | Fetch and merge remote changes. |
git pull --rebase | Fetch and replay your commits on top. |
git push | Send local commits to the remote. |
git push -u origin main | Push and set the upstream branch. |
git push origin --delete old-branch | Delete a branch on the remote. |
git push --tags | Send local tags to the remote. |
git remote show origin | Show details about a remote. |
git push --force-with-lease | Replace remote history with a safety check. |
Undo and Recover
| Code | What it does |
|---|---|
git restore file.txt | Throw away changes in a file. |
git restore . | Throw away all unstaged changes. |
git reset HEAD file.txt | Unstage a file and keep changes. |
git reset --soft HEAD~1 | Undo the last commit and keep changes staged. |
git reset --mixed HEAD~1 | Undo the last commit and unstage changes. |
git reset --hard HEAD~1 | Undo the last commit and delete changes. |
git revert HEAD | Make a new commit that undoes the last. |
git revert abc1234 | Undo a chosen commit with a new commit. |
git reflog | Show where HEAD has been. |
git checkout abc1234 -- file.txt | Restore one file from an old commit. |
git stash | Save changes for later and clean the tree. |
git stash pop | Restore stashed changes and drop them. |
Tags and Releases
| Code | What it does |
|---|---|
git tag | List all tags. |
git tag v1.0.0 | Create a light tag. |
git tag -a v1.0.0 -m "Release 1.0" | Create an annotated tag with a message. |
git show v1.0.0 | Show tag details and its commit. |
git tag -d v1.0.0 | Delete a local tag. |
git push origin v1.0.0 | Send one tag to the remote. |
git push origin --delete v1.0.0 | Delete a tag on the remote. |
git checkout v1.0.0 | Move to the commit of a tag. |
git describe --tags | Show the nearest tag for HEAD. |
git tag -l "v1.*" | List tags that match a pattern. |
git tag v1.0.1 abc1234 | Tag an older commit. |
git fetch --tags | Download tags from the remote. |
Cleanup and Checks
| Code | What it does |
|---|---|
git clean -n | Preview files that clean will delete. |
git clean -f | Delete untracked files. |
git clean -fd | Delete untracked files and folders. |
git gc | Clean up and compress the repository. |
git fsck | Check the repository for errors. |
git log --stat | Show history with file change counts. |
git shortlog -sn | Count commits by author. |
git blame file.txt | Show who changed each line. |
git grep "text" | Search tracked files for text. |
git count-objects -v | Show repository size details. |
git archive -o out.zip HEAD | Export the current tree as a zip file. |
git help -a | List all available Git commands. |