π Git Local β Remote Cheat Sheet¶
π Core ideas¶
- clone β make a new local folder from remote
- init β make current folder a repo
- remote = nickname + URL (
origin) - upstream = default remote branch your local branch tracks
π Common Scenarios¶
1. Start from Remote β Local¶
β
Now just git pull and git push
2. Start from Local β Remote¶
git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin <REMOTE-URL>
git push -u origin main
The
-uflag sets the upstream for your branch. It tells Git: βmainshould trackorigin/mainfrom now on.β
3. Already Have Both (Sync Them)¶
If unrelated histories:
π Understanding βUpstreamβ¶
The upstream branch is what your local branch βtracks.β
When you set it, git pull and git push automatically know which remote branch to sync with β no need to retype origin main every time.
git branch -vv # see tracking info
git push -u origin main # set upstream during first push
git branch --set-upstream-to=origin/main main # set manually
After setting upstream:
If you ever clone a repo, upstreams are usually set for you. Youβll only need to set them manually when creating new branches or pushing from a freshly inited local repo.
Think of it this way:
originis the remote name (like a server nickname).origin/mainis the remote branch. The upstream is your local branchβs connection to it.
π Folder Rules¶
git clone <url>β makes a new foldergit clone <url> myfolderβ makes myfoldergit clone <url> .β current empty folder- Always run Git inside the repo folder
π€ Which First?¶
- Solo / quick test: local β push
- Team / templates / CI: remote β clone
β‘ Handy Commands¶
git remote -v # list remotes
git push -u origin main # first push, set upstream
git pull --rebase # cleaner pulls
git remote set-url origin <url> # change remote
git branch -vv # show tracking branches
β οΈ Pitfalls¶
- Remote has README β
git pull --rebase --allow-unrelated-histories - Wrong branch name β
git branch -M main - Tracked junk files β fix
.gitignore, then:
ποΈ Decision Tree¶
- Have remote? β
git clone <url> - Have local only? β
git initβgit push -u origin main - Both exist? β
git fetchβgit pull --rebaseβgit push
π This is the minimal flow youβll use 99% of the time.
```
Why this matters¶
Without an upstream, every git pull or git push must specify remote + branch manually (git push origin main). Once set, you can just type git push. Thatβs why -u is quietly one of the most powerful flags in Git β it wires your local branch into the network.