# Git Quick Guide A small reference for the Git commands I forget five minutes after using them. ## Status See changed, staged, and untracked files: ```sh git status ``` Short version: ```sh git status -sb ``` ## Start a Repository ```sh git init ``` Set the main branch name: ```sh git branch -M main ``` ## Clone ```sh git clone ``` Example with SSH: ```sh git clone git@github.com:user/repo.git ``` ## Stage Changes Stage one file: ```sh git add file.c ``` Stage everything: ```sh git add . ``` Unstage a file: ```sh git restore --staged file.c ``` ## Commit ```sh git commit -m "Describe the change" ``` Amend the previous commit: ```sh git commit --amend ``` Change only its message: ```sh git commit --amend -m "New message" ``` ## Remotes Show remotes: ```sh git remote -v ``` Add GitHub as origin: ```sh git remote add origin \ git@github.com:user/repo.git ``` Change origin: ```sh git remote set-url origin \ git@github.com:user/repo.git ``` ## Push First push: ```sh git push -u origin main ``` Normal push afterward: ```sh git push ``` Force local branch onto remote: ```sh git push --force-with-lease origin main ``` Prefer `--force-with-lease` over plain `--force`. ## Pull / Fetch Download and merge remote changes: ```sh git pull ``` Download changes without merging: ```sh git fetch ``` See remote branches: ```sh git branch -r ``` ## Branches List branches: ```sh git branch ``` Create a branch: ```sh git branch new-feature ``` Create and switch: ```sh git switch -c new-feature ``` Switch branches: ```sh git switch main ``` Delete local branch: ```sh git branch -d new-feature ``` Force-delete local branch: ```sh git branch -D new-feature ``` ## Differences Unstaged changes: ```sh git diff ``` Staged changes: ```sh git diff --staged ``` Compare branches: ```sh git diff main..other-branch ``` ## History Compact history: ```sh git log --oneline ``` Pretty branch graph: ```sh git log --oneline --graph --decorate --all ``` Show one commit: ```sh git show ``` ## Undo Local Changes Discard changes to one file: ```sh git restore file.c ``` Discard ALL unstaged changes: ```sh git restore . ``` WARNING: those changes are gone. ## Undo a Commit Undo the last commit but keep changes staged: ```sh git reset --soft HEAD~1 ``` Undo commit and leave changes unstaged: ```sh git reset HEAD~1 ``` Safely undo an already-pushed commit: ```sh git revert ``` ## Remove Files Delete file and remove it from Git: ```sh git rm file ``` Stop tracking but keep local file: ```sh git rm --cached file ``` Useful after adding something to `.gitignore`. ## .gitignore Example: ```text *.o *.core bin/ obj/ secret.txt ``` If Git already tracks the file: ```sh git rm --cached file ``` then commit the change. ## Stash Temporarily hide current changes: ```sh git stash ``` Include untracked files: ```sh git stash -u ``` Restore latest stash: ```sh git stash pop ``` List stashes: ```sh git stash list ``` ## Tags Create a tag: ```sh git tag v1.0.0 ``` Annotated tag: ```sh git tag -a v1.0.0 -m "Version 1.0.0" ``` Push one tag: ```sh git push origin v1.0.0 ``` Push all tags: ```sh git push --tags ``` ## Release Snapshot Create an archive from `main`: ```sh git archive \ --format=tar.gz \ --output=project.tar.gz \ main ``` ZIP version: ```sh git archive \ --format=zip \ --output=project.zip \ main ``` ## Useful Recovery Commands What the hell did I just do? ```sh git reflog ``` Find a previous HEAD: ```sh git reflog ``` Then inspect it: ```sh git show HEAD@{1} ``` Git loses surprisingly little. `reflog` is often the place to look before declaring the repository deceased. .