A compact, practical Git reference — the commands you use every day, plus the ones you need when something goes wrong.
Setup
git config --global user.name "Alice Example"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
git config --global pull.rebase true # rebase instead of merge commits on pull (optional)
git config --global core.editor nano
git config --list
Start a repository
git init
git clone https://github.com/org/repo.git
git clone [email protected]:org/repo.git # SSH — see Git SSH keys
git clone --depth 1 URL # shallow clone, latest commit only
Status and differences
git status
git status -s # short format
git diff # unstaged changes
git diff --staged # staged changes
git diff main..feature # between branches
git diff HEAD~3 -- path/file # one file vs 3 commits ago
Staging and committing
git add file.txt
git add . # everything in the current directory
git add -p # choose hunks interactively
git commit -m "Add login form"
git commit -am "Fix typo" # stage tracked files and commit
git commit --amend # change the last commit (message or content)
Only amend commits you haven’t pushed yet — or that nobody else has pulled.
Branches
git branch # list local branches
git branch -a # include remote branches
git switch -c feature/login # create and switch
git switch main
git branch -d feature/login # delete (merged)
git branch -D feature/login # force delete
git branch -m old new # rename
(git checkout does the same jobs in older tutorials; switch and restore are clearer.)
Merging and rebasing
git switch main
git merge feature/login # merge a branch
git merge --no-ff feature/login # always create a merge commit
git rebase main # replay current branch on top of main
git rebase -i HEAD~4 # tidy the last 4 commits (squash, reword, reorder)
git cherry-pick a1b2c3d # copy one commit onto the current branch
Conflicts: edit the files, git add them, then git merge --continue or git rebase --continue; or abort with --abort. See merge vs rebase.
Remotes
git remote -v
git remote add origin [email protected]:org/repo.git
git fetch # download, don't change your branches
git pull # fetch + merge/rebase
git push -u origin feature/login # first push of a new branch
git push
git push --force-with-lease # safer force push after a rebase
git push origin --delete feature/login
Prefer --force-with-lease over --force: it refuses if someone else pushed in the meantime.
Stash
git stash # put uncommitted changes aside
git stash -u # include untracked files
git stash list
git stash pop # re-apply and drop
git stash apply stash@{1}
See git stash.
History
git log --oneline --graph --decorate --all
git log -p path/file # changes to a file
git log --author="Alice" --since="2 weeks ago"
git log -S "functionName" # commits that added/removed a string
git blame path/file # who changed each line
git show a1b2c3d # one commit
git reflog # where HEAD has been — your safety net
Undoing things
git restore file.txt # discard unstaged changes to a file
git restore --staged file.txt # unstage
git reset --soft HEAD~1 # undo last commit, keep changes staged
git reset --mixed HEAD~1 # undo last commit, keep changes unstaged
git reset --hard HEAD~1 # undo last commit and discard changes (careful!)
git revert a1b2c3d # new commit that undoes a1b2c3d (safe for shared history)
git clean -fd # delete untracked files and directories (careful!)
Rule: reset rewrites history (fine locally), revert adds history (safe after pushing). Lost something? git reflog usually still has it. Full guide: undo the last commit.
Tags and releases
git tag v1.2.0
git tag -a v1.2.0 -m "Release 1.2.0"
git push origin v1.2.0
git push --tags
.gitignore
node_modules/
.env
*.log
dist/
Already tracked a file you now want ignored? git rm --cached file. See the .gitignore guide.
Handy aliases
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.undo "reset --soft HEAD~1"
Self-host your repositories with Gitea, and see Git SSH keys for GitHub for passwordless pushes.