git CLI cheatsheet.

Init / clone

git init
git clone https://github.com/me/repo
git clone --depth 1 url             # shallow
git clone --branch dev url

status

git status
git status -s                       # short
git status -sb                      # with branch

Stage / commit

git add file
git add .                           # all changes in dir
git add -p                          # interactive (hunks)
git add -A                          # all changes incl deletions

git commit -m "msg"
git commit -am "msg"                # add tracked + commit
git commit --amend                  # edit last (don't push!)
git commit --amend --no-edit
git commit --fixup=<hash>           # for autosquash

Diff

git diff                            # unstaged
git diff --staged                   # staged
git diff HEAD                       # all uncommitted
git diff main..feature
git diff main...feature             # since branched
git diff --stat
git diff HEAD~3                     # vs 3 commits back

Branch

git branch
git branch -a                       # incl remote
git branch -vv                      # tracking
git branch new-feature
git checkout new-feature
git checkout -b new-feature
git switch new-feature              # new way
git switch -c new-feature

git branch -d branch                # delete (merged only)
git branch -D branch                # force
git branch -m old new               # rename

Log

git log
git log --oneline
git log --graph --oneline --all
git log -p                          # with diff
git log -n 10
git log --since "1 week ago"
git log --author="Alice"
git log --grep="bug"
git log file
git log -L :func:file

Show

git show HEAD
git show HEAD~3
git show <hash>
git show --stat <hash>

Reset / restore

git restore file                    # discard changes
git restore --staged file           # unstage

git reset HEAD~                     # uncommit, keep changes
git reset --soft HEAD~              # uncommit, keep staged
git reset --hard HEAD~              # discard
git reset --hard origin/main        # match remote

Stash

git stash
git stash push -m "wip"
git stash list
git stash pop
git stash apply stash@{2}
git stash drop stash@{2}
git stash --keep-index              # stash unstaged only
git stash --include-untracked

Remote

git remote -v
git remote add origin url
git remote set-url origin url
git remote rename origin upstream

Push / pull / fetch

git fetch
git fetch --all
git pull
git pull --rebase                   # avoid merge commits
git push
git push -u origin feature          # set upstream
git push --force-with-lease         # safer force push

Rebase

git rebase main
git rebase -i HEAD~5                # interactive
git rebase --continue
git rebase --abort
git rebase --onto target from to
git pull --rebase

In -i mode:

pick / p     keep
reword / r   change msg
edit / e     pause for amend
squash / s   merge into prev with msg
fixup / f    merge silently
drop / d     remove

Cherry-pick

git cherry-pick <hash>
git cherry-pick A..B                # range exclusive of A
git cherry-pick --continue

Merge

git merge feature
git merge --no-ff feature           # always merge commit
git merge --squash feature
git merge --abort

Tag

git tag                             # list
git tag v1.0
git tag -a v1.0 -m "msg"            # annotated
git tag -s v1.0                     # signed
git push --tags
git push origin v1.0
git tag -d v1.0
git push --delete origin v1.0

Blame

git blame file
git blame -L 10,20 file
git blame -w                        # ignore whitespace

Bisect (find bad commit)

git bisect start
git bisect bad
git bisect good v1.0
# git checks out middle commit; you test
git bisect good                     # or bad
# ... until found
git bisect reset

Reflog (recover lost commits)

git reflog
git reset --hard HEAD@{2}           # recover state

Clean

git clean -n                        # dry run
git clean -fd                       # delete untracked files + dirs
git clean -fdx                      # also gitignored

Submodules

git submodule add url path
git submodule update --init --recursive
git submodule update --remote

Most teams prefer to avoid submodules.

Worktree

git worktree add ../feature feature
git worktree list
git worktree remove ../feature

Work on multiple branches without stashing.

config

git config --global user.name "Alice"
git config --global user.email [email protected]
git config --global core.editor nvim
git config --global init.defaultBranch main
git config --global pull.rebase true
git config --global push.autoSetupRemote true
git config --global rerere.enabled true

Aliases

git config --global alias.co checkout
git config --global alias.st 'status -sb'
git config --global alias.lg "log --graph --oneline --all --decorate"
git config --global alias.amend "commit --amend --no-edit"

.gitignore

node_modules/
*.log
.env
.DS_Store

.gitattributes

* text=auto
*.sh text eol=lf
*.bat text eol=crlf
*.png binary

Hooks

.git/hooks/pre-commit:

#!/bin/sh
make lint || exit 1
chmod +x .git/hooks/pre-commit

Or use husky / pre-commit.

Recovery

DisasterFix
Accidental commitgit reset HEAD~
Force pushed over team’s workgit reflog on their machine
Deleted branchgit reflog + git branch new <hash>
Dirty mergegit merge --abort
Wrong commit messagegit commit --amend (if not pushed)

Common mistakes

  • git push --force to shared branch.
  • git pull with merge → ugly history. Use --rebase.
  • git commit -am adds only tracked → new files missed.
  • Editing history of pushed branch.
  • Big commits — break up.

Read this next

If you want my git config + workflow, it’s at rajpoot.dev .


Building something AI-, backend-, or data-heavy and want a second pair of eyes? I do consulting and freelance work — see my projects and ways to reach me at rajpoot.dev .