Level 2 — Working in a team · wrap-up
Cheat sheet & self-check
Every command from this section on one page.
Remotes
git remote -v | Where fetch and push go |
git remote add upstream <url> | Add the original repository when working from a fork |
git fetch --all --prune | Download all remote updates, drop deleted branches |
git pull --rebase | Fetch and replay your local commits on top |
git push -u origin feature/x | Push a new branch and set its upstream |
git push --force-with-lease | Overwrite your own rewritten branch safely |
Authentication
ssh-keygen -t ed25519 -C you@example.com | Create an SSH key for the Git server |
ssh -T git@github.com | Test SSH authentication |
git config --global credential.helper cache | Cache HTTPS credentials in memory for a while |
Tags and releases
git tag -a v1.4.0 -m "Release 1.4.0" | Annotated release tag |
git push origin v1.4.0 | Publish one tag (git push --tags publishes all) |
git describe --tags | Nearest tag and commits since it, e.g. v1.4.0-3-g3f1c9a0 |
git log v1.3.0..v1.4.0 --oneline | What changed between two releases |
Release branches and hotfixes
git switch -c release/1.4 v1.4.0 | Start a release branch from a tag |
git cherry-pick -x <sha> | Copy a fix onto another branch, recording where it came from |
git branch --contains <sha> | Which branches already include a commit? |
Undo, by situation
git restore <file> | Discard unstaged changes in a file |
git restore --staged <file> | Unstage, keep the changes |
git reset --soft HEAD~1 | Undo the last commit, keep its changes staged |
git reset --hard origin/main | Throw away local commits and changes, match the remote |
git revert <sha> | New commit that undoes <sha> (safe on shared branches) |
git revert -m 1 <merge-sha> | Revert a merge commit |
Park, move, find
git stash push -m "wip login" | Park uncommitted work |
git stash list / git stash pop | See / restore parked work |
git cherry-pick <sha> | Copy one commit onto the current branch |
git reflog | Everywhere HEAD has been, including 'lost' commits |
git branch rescue <sha> | Give a lost commit a branch again |
git bisect start; git bisect bad; git bisect good v1.3.0 | Binary-search for the commit that broke something |