Skip to content

Cheatsheet

The Git commands used most often, grouped by task, with no collapsible blocks so the page prints on one sweep. Each group links to the topic that explains the commands in full. Commands are cross-platform unless a note says otherwise.


Setup and Config

git config --global user.name "Amina Yusuf"     # identity for commits
git config --global user.email "amina@example.com"
git config --global init.defaultBranch main     # default branch name
git config --global pull.rebase true            # pull rebases instead of merging
git config --list --show-origin                 # every setting and which file set it
git config --global alias.lg "log --oneline --graph --all"   # a shortcut

See Install and Config.


Creating and Cloning

git init                       # start a repo in the current directory
git clone <url>                # clone a remote
git clone --depth 1 <url>      # shallow: only the latest commit
git clone --filter=blob:none <url>       # partial: blobs on demand
git clone --recurse-submodules <url>     # populate submodules too

See What Is Git and Large Repos.


Staging and Committing

git status                     # what is staged, modified, untracked
git status -sb                 # short form with branch and ahead/behind
git add <path>                 # stage a file
git add -p                     # stage selected hunks interactively
git commit -m "message"        # commit the staged changes
git commit --amend             # replace the last commit (unpushed only)
git restore --staged <path>    # unstage, keep the working-tree change

See Staging and Committing and The Three Trees.


Inspecting and Comparing

git log --oneline --graph --all    # compact history with branches
git log -p -- <path>               # history of one file with diffs
git show <sha>                     # a commit's message and diff
git diff                           # working tree vs index
git diff --staged                  # index vs HEAD
git diff A..B                      # commits reachable from B but not A
git blame <path>                   # who last changed each line

See Inspecting History.


Branching and Switching

git branch                     # list local branches
git switch <branch>            # change branch
git switch -c <new>            # create and switch
git branch -d <branch>         # delete a merged branch (-D to force)
git branch -m <old> <new>      # rename
git switch --detach <sha>      # check out a commit without a branch

See Branches.


Merging and Rebasing

git merge <branch>             # three-way merge into the current branch
git merge --no-ff <branch>     # force a merge commit
git merge --abort              # back out of a conflicted merge
git rebase <base>              # replay current branch onto base
git rebase -i <base>           # squash, reorder, reword, drop
git rebase --continue          # after resolving a conflict
git cherry-pick <sha>          # apply one commit here

See Merging, Rebasing, Interactive Rebase and Cherry-Pick.


Undoing and Recovering

git restore <path>             # discard a working-tree change
git reset --soft HEAD~1        # undo commit, keep changes staged
git reset --mixed HEAD~1       # undo commit, keep changes unstaged (default)
git reset --hard HEAD~1        # undo commit and discard changes
git revert <sha>               # a new commit that inverts a pushed one
git reflog                     # every move of HEAD, to recover lost commits
git clean -fd                  # delete untracked files and directories

See Undoing Changes and Reflog and Recovery.


Remotes: Fetch, Push, Pull

git remote -v                  # list remotes and URLs
git remote add origin <url>    # add a remote
git fetch origin               # download refs and objects, change nothing
git pull                       # fetch plus merge (or rebase, if configured)
git pull --rebase              # fetch then replay your commits on top
git push -u origin <branch>    # push and set upstream
git push --force-with-lease    # overwrite your own branch safely

See Remotes and Pushing and Pulling.


Tags and Stash

git tag                        # list tags
git tag -a v1.2.0 -m "Release" # annotated tag
git push origin v1.2.0         # push one tag (--tags for all)
git describe --tags            # nearest tag plus distance
git stash                      # shelve working-tree changes
git stash pop                  # reapply and drop the latest stash
git stash list                 # see shelved changes

See Tags and Releases and Stashing.


History Rewriting and Cleanup

git rebase -i <base>           # reshape local commits before sharing
git filter-repo --path <file> --invert-paths    # purge a file from all history
git filter-repo --replace-text rules.txt        # scrub a string from history
git gc --prune=now             # repack and drop unreachable objects
git fsck                       # verify object integrity

See Rewriting History, Filter-Repo and Secrets and Packfiles and GC.


Advanced Tooling

git bisect start <bad> <good>  # binary-search for a regression
git bisect run ./test.sh       # automate the search
git worktree add ../hotfix -b hotfix    # a second checkout on a new branch
git submodule update --init --recursive # populate submodules after clone
git sparse-checkout set <dir>  # materialize only chosen paths
git log --show-signature -1    # verify a commit's signature

See Bisect, Worktrees, Submodules and Credentials and Signing.