Git Cheat Sheet: Essential and Advanced Commands
·6 mins
Table of Contents
Quick Guide #
Basic (most used) #
Command | Description |
---|---|
git init <folder> |
Creates an empty repository in <folder> . Without arguments initializes the current folder as a repository. |
git clone <repository> |
Clones a <repository> to your local machine. The <repository> can be local or remote via HTTP or SSH. |
git config user.name <author> |
Sets the <author> for all commits in the current repository. |
git add <folder/file> |
Adds the changes made in the specified `<folder |
git commit -m "<message>" |
Commits changes immediately using <message> as the commit message, without opening an editor. |
git status |
Shows the list of staged, unstaged, and untracked files. |
git log |
Shows the commit history (default format). For customization see the additional options section. |
git diff |
Shows unstaged changes between the index and working directory. |
git stash |
Temporarily saves uncommitted changes (both staged and unstaged). |
git reset |
Removes files from the staging area without losing changes. |
git rm <file> |
Removes a <file> from the repository and disk. |
git mv <old-file> <new-file> |
Renames or moves a file. |
git tag <name> |
Creates a lightweight tag on the current commit. |
git show |
Shows details about Git objects such as commits, tags, or files. |
Rewriting History #
Command | Description |
---|---|
git commit --amend |
Replaces the last commit and/or staged changes. Without staged changes, it edits the last commit message. |
git rebase <base> |
Reapplies commits on top of the new <base> , reorganizing history to keep it linear and clear. |
git reflog |
Shows a log of changes to the local repository’s HEAD . Add --relative-date to show dates or --all for all references. |
Branches #
Command | Description |
---|---|
git branch <branch> |
Creates a new <branch> . Without arguments, shows the list of repository branches. |
git checkout -b <branch> |
Creates a new <branch> based on the current branch. Without -b switches to the <branch> . |
git checkout <branch> |
Switches to the specified <branch> . |
git switch <branch> |
Switches to the specified <branch> . More intuitive than git checkout for changing branches. |
git switch -c <branch> |
Creates a new <branch> and switches to it. Equivalent to git checkout -b <branch> . |
git merge <branch> |
Merges the <branch> into the current branch. |
Remote Repositories #
Command | Description |
---|---|
git remote add <name> <url> |
Adds a new connection to a remote repository. <name> is the alias for the <url> . |
git fetch <remote> <branch> |
Fetches the <branch> from the <remote> repository. If branch is omitted, fetches all branches. |
git pull <remote> <branch> |
Fetches from the <remote> repository and merges immediately with the local repository. |
git push <remote> <branch> |
Pushes the <branch> to the <remote> , updating it if it exists or creating it otherwise. |
Additional Options #
git config #
Command | Description |
---|---|
git config --global user.name <author> |
Sets the <author> name for all commits for the current user. |
git config --global user.email <email> |
Sets the <email> for all commits for the current user. |
git config --global alias.<alias> <command> |
Creates a shortcut <alias> for a Git <command> . Example: alias.glog log –graph means glog ⇒ git log --graph . |
git config --system core.editor <editor> |
Sets the <editor> used by Git commands for all users (e.g. vim ). |
git config --global --edit |
Opens the global config file for manual editing using the default editor (e.g. nano ). |
git log #
Command | Description |
---|---|
git log -<limit> |
Limits the number of logs shown to <limit> . Example: git log -5 shows the last 5 commits. |
git log --oneline |
Condenses each commit to a single line. |
git log -p |
Shows all changes for each commit. |
git log --stat |
Includes which files changed and relative number of lines added or removed per file. |
git log --author="<author>" |
Searches commits by a particular <author> . |
git log --grep="<pattern>" |
Searches commits whose messages match <pattern> . |
git log <from>..<to> |
Shows commits between <from> and <to> . Can use commit hashes, branch names, HEAD or any revision point. |
git log -- <file> |
Shows commits affecting the specified <file> . |
git log --graph --decorate |
--graph shows a text graph on the left side; --decorate shows branch or tag names. |
git diff #
Command | Description |
---|---|
git diff HEAD |
Shows differences between the last commit and the working directory. |
git diff --cached |
Shows differences between the last commit and the staged changes. |
git reset #
Command | Description |
---|---|
git reset |
Resets the staging area to match the last commit but leaves working directory unchanged. |
git reset --hard |
Resets staging area and working directory to last commit, discarding all changes. |
git reset <commit> |
Moves the current branch to <commit> , resetting staging area but leaving working directory unchanged. |
git reset --hard <commit> |
Same as git reset --hard but discards all uncommitted changes and commits after <commit> . |
git rebase #
Command | Description |
---|---|
git rebase -i <base> |
Interactive rebase that opens an editor to modify commands for each commit to be applied to new <base> . |
Undo Changes #
Command | Description |
---|---|
git checkout -- <file> |
Discards local changes in <file> , restoring it to last commit state. |
git restore <file> |
New recommended way to discard changes in a file. |
git reset HEAD <file> |
Removes <file> from staging but keeps changes in working directory. |
git revert <commit> |
Creates a new commit that undoes changes introduced by <commit> . |
git clean -f |
Removes untracked files in working directory (useful to clean junk files). |
git stash #
Command | Description |
---|---|
git stash |
Temporarily saves uncommitted changes and cleans working directory. |
git stash pop |
Restores the last stash and removes it from the stash list. |
git stash apply |
Restores the last stash without removing it from the stash list. |
git stash list |
Lists all saved stashes. |
git stash drop |
Deletes the most recent (or specified) stash. |
git pull #
Command | Description |
---|---|
git pull --rebase <remote> |
Fetches from <remote> for the current branch but rebases instead of merging. |
git push #
Command | Description |
---|---|
git push --force |
Forces pushing branch to <remote> , ignoring conflicts. Use only if sure. |
git push --all <remote> |
Pushes all local branches to <remote> . |
git push --tags <remote> |
Tags are not pushed automatically; use --tags to push them to <remote> . |
Tags (git tag) #
Command | Description |
---|---|
git tag |
Lists all existing tags. |
git tag <name> |
Creates a lightweight tag on the current commit. |
git tag -a <name> -m "<message>" |
Creates an annotated tag with a message. |
git show <tag> |
Shows detailed information about a tag. |
git push <remote> <tag> |
Pushes the tag to the remote repository. |
git rm and git mv #
Command | Description |
---|---|
git rm <file> |
Removes the file from the repository and disk. |
git rm --cached <file> |
Removes the file from the staging area but keeps it on disk. |
git mv <old-file> <new-file> |
Moves or renames files. |