TechSchool
Home / Cheat sheets / Git

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

CodeWhat it does
git --versionShow 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 --listShow all current config settings.
git config --global core.editor nanoSet nano as the default editor.
git config --global init.defaultBranch mainMake main the default branch name.
git help commitOpen the manual page for a command.
git config --global --unset user.nameRemove one config setting.
git config --global color.ui autoTurn on colored output.
git config --global pull.rebase falseUse merge when you pull.
git config --global alias.st statusCreate a short name for a command.
git config --global --editOpen the config file in your editor.

Start a Repository

CodeWhat it does
git initCreate a new repository in this folder.
git init my-projectCreate a new repository in a new folder.
git clone https://host/repo.gitCopy a remote repository to your machine.
git clone https://host/repo.git mydirCopy a repository into a named folder.
git statusShow changed, staged, and untracked files.
git status -sShow a short status list.
git add file.txtStage one file for the next commit.
git add .Stage all changes in this folder.
git add -AStage all changes in the repository.
git rm file.txtRemove a file and stage the removal.
git mv old.txt new.txtRename a file and stage the change.
git restore --staged file.txtUnstage a file and keep its changes.

Save Changes

CodeWhat 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-editAdd staged files to the last commit.
git logShow the commit history.
git log --onelineShow history as one line per commit.
git log --oneline --graphShow history as a simple graph.
git log -p file.txtShow changes to one file over time.
git show HEADShow the last commit and its changes.
git diffShow unstaged changes.
git diff --stagedShow staged changes.
git diff HEAD~1 HEADCompare the last two commits.

Branches

CodeWhat it does
git branchList local branches.
git branch -aList local and remote branches.
git branch feature-loginCreate a new branch.
git switch feature-loginMove to an existing branch.
git switch -c feature-loginCreate a branch and move to it.
git switch -Move back to the previous branch.
git branch -d feature-loginDelete a merged branch.
git branch -D feature-loginForce delete a branch.
git branch -m new-nameRename the current branch.
git merge feature-loginJoin another branch into the current one.
git merge --abortStop a merge and return to the old state.
git branch --mergedList branches already merged here.

Remote Work

CodeWhat it does
git remote -vList remote names and URLs.
git remote add origin https://host/repo.gitAdd a remote named origin.
git remote remove originRemove a remote.
git fetch originDownload remote changes but do not merge.
git pullFetch and merge remote changes.
git pull --rebaseFetch and replay your commits on top.
git pushSend local commits to the remote.
git push -u origin mainPush and set the upstream branch.
git push origin --delete old-branchDelete a branch on the remote.
git push --tagsSend local tags to the remote.
git remote show originShow details about a remote.
git push --force-with-leaseReplace remote history with a safety check.

Undo and Recover

CodeWhat it does
git restore file.txtThrow away changes in a file.
git restore .Throw away all unstaged changes.
git reset HEAD file.txtUnstage a file and keep changes.
git reset --soft HEAD~1Undo the last commit and keep changes staged.
git reset --mixed HEAD~1Undo the last commit and unstage changes.
git reset --hard HEAD~1Undo the last commit and delete changes.
git revert HEADMake a new commit that undoes the last.
git revert abc1234Undo a chosen commit with a new commit.
git reflogShow where HEAD has been.
git checkout abc1234 -- file.txtRestore one file from an old commit.
git stashSave changes for later and clean the tree.
git stash popRestore stashed changes and drop them.

Tags and Releases

CodeWhat it does
git tagList all tags.
git tag v1.0.0Create a light tag.
git tag -a v1.0.0 -m "Release 1.0"Create an annotated tag with a message.
git show v1.0.0Show tag details and its commit.
git tag -d v1.0.0Delete a local tag.
git push origin v1.0.0Send one tag to the remote.
git push origin --delete v1.0.0Delete a tag on the remote.
git checkout v1.0.0Move to the commit of a tag.
git describe --tagsShow the nearest tag for HEAD.
git tag -l "v1.*"List tags that match a pattern.
git tag v1.0.1 abc1234Tag an older commit.
git fetch --tagsDownload tags from the remote.

Cleanup and Checks

CodeWhat it does
git clean -nPreview files that clean will delete.
git clean -fDelete untracked files.
git clean -fdDelete untracked files and folders.
git gcClean up and compress the repository.
git fsckCheck the repository for errors.
git log --statShow history with file change counts.
git shortlog -snCount commits by author.
git blame file.txtShow who changed each line.
git grep "text"Search tracked files for text.
git count-objects -vShow repository size details.
git archive -o out.zip HEADExport the current tree as a zip file.
git help -aList all available Git commands.
More on TechSchool
See the Git interview questions Python JavaScript TypeScript HTML CSS React Vue 3 Node.js Java PHP C C++ C# Go SQL MongoDB Redis Linux Docker Data Structures