Git

Setting Up

git init
git clone <remote repository url>
git remote add origin <remote repository url>

Staging

git status
git add <file 1> ... <file N>
git restore --staged <file 1> ... <file N>

Committing

git commit 
Note

Scoped commit message style:

<scope>: <what changed>

<why the change was made or extra context>

<Key: value>
git commit --amend -m "<new commit message>"

Branching

git branch
git branch -r 
git branch <branch>
git branch -m <branch to rename> <new branch name>
git switch <branch>
git branch -d <branch>
git branch -D <branch>
git push origin --delete <remote branch>

Inspecting & Comparing

git log
git log --oneline --graph --all
git diff
git diff --staged
git diff <branch or commit A> <branch or commit B>
git blame <file>
git show <commit>
git switch --detach <commit> or HEAD<relative reference>
Note

Checking out a commit causes the HEAD pointer to be in a detached head state, where any commits won't belong to any branches, and will be lost when you switch away (unless you run git switch -c <new branch> first to keep them).

Merging & Rebasing

git merge <from branch>
git rebase <from branch>
Note

Squashing is done inside an interactive rebase: mark commits with squash (or s) to combine them into the commit above.

git merge --abort
git rebase --abort
Note

When to use rebase vs merge:

  • Updating your private stuff from upstream: rebase
  • Landing your stuff into shared branches: merge

Stashing

git stash
git stash list
git stash show stash@{<index>}
git stash pop
git stash apply
git stash drop

Undoing Things

git reset [--soft | --mixed | --hard] <commit>
git revert <commit>

Syncing

git fetch <remote>
git pull --rebase

Pushing

git push <remote> <branch>
git push -u origin <branch>