πŸ“ 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

git clone <REMOTE-URL> [folder]
cd [folder]

βœ… 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 -u flag sets the upstream for your branch. It tells Git: β€œmain should track origin/main from now on.”


3. Already Have Both (Sync Them)

git remote -v
git fetch origin
git switch main
git pull --rebase
git push

If unrelated histories:

git pull --rebase --allow-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:

git pull     # pulls from origin/main automatically
git push     # pushes to origin/main automatically

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: origin is the remote name (like a server nickname). origin/main is the remote branch. The upstream is your local branch’s connection to it.


πŸ“‚ Folder Rules

  • git clone <url> β†’ makes a new folder
  • git clone <url> myfolder β†’ makes myfolder
  • git 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:
git rm -r --cached .
git add .
git commit -m "clean tracked files"

πŸ—‚οΈ 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.