Git & Terminal Cheatsheet
Remotes and Syncing
Use this Git & Terminal reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Managing Remotes
git remote -v # list remotes and their URLs git remote add origin <url> # register a remote git remote set-url origin <url> # change its URL (e.g. https -> ssh) git remote rename origin gh # rename git remote remove gh # remove git remote show origin # branches, tracking, push/pull status
Fetch vs Pull
git fetch # download remote commits, touch NOTHING local git fetch --all --prune # all remotes, drop refs to deleted branches git pull # fetch + merge the upstream into your branch git pull --rebase # fetch + replay your local commits on top (linear) git pull --ff-only # fetch + fast-forward only, error if diverged
git fetch is always safe. git pull is fetch plus an integration step, and which step (merge vs rebase) is worth configuring once:
git config --global pull.rebase true # make plain `git pull` rebase git config --global pull.ff only # or: refuse anything but fast-forward
Pushing
git push -u origin main # first push: create upstream link, then push git push # after -u, this is all you need git push origin v1.2.0 # push one tag git push --tags # push all tags git push origin --delete feature # delete a remote branch
The -u (--set-upstream) link is what lets bare git push / git pull / git status know which remote branch you track. Check with git branch -vv, fix with git branch --set-upstream-to=origin/main.
When Branches Have Diverged
git push rejected with non-fast-forward, or git pull says You have divergent branches? You and the remote both have new commits. Pick an integration strategy:
git pull --rebase # usual choice: your commits replay on top, no merge commit git pull --no-rebase # or merge: creates a merge commit git status # confirm "ahead X, behind Y" is resolved git push
If the rebase hits conflicts: fix files, git add, git rebase --continue (or git rebase --abort to undo the pull).
Force-Pushing Safely
After you rebase or amend commits that were already pushed, the remote rejects a normal push. Never plain --force on a shared branch:
git push --force-with-lease # push, but FAIL if someone else pushed meanwhile git push --force-with-lease origin feature git push --force # overwrites unconditionally, can delete teammates' work
--force-with-lease refuses to overwrite commits you have not seen, which makes it the default choice for updating your own rebased PR branch.
Syncing a Fork
Your fork is origin, the original repo is conventionally upstream:
git remote add upstream <original-url>
git fetch upstream
git switch main
git merge upstream/main # or: git rebase upstream/main
git push origin mainQuick Reference
| Task | Command |
|---|---|
| Download only | git fetch --all --prune |
| Update branch | git pull (or git pull --rebase) |
| First push | git push -u origin <branch> |
| Push | git push |
| Push after rebase/amend | git push --force-with-lease |
| Delete remote branch | git push origin --delete <branch> |
| See tracking info | git branch -vv |
| Sync a fork | git fetch upstream && git merge upstream/main |