Remotes and collaboration
Cloning, pushing, fetching versus pulling, and understanding what origin/main actually points at.
Connecting
git clone [email protected]:user/repo.git
cd repo
git remote -v
git remote add upstream <url>
git remote set-url origin <new-url>| Ref | Meaning |
|---|---|
origin/main | Last known position of the remote's main branch |
main | Your local branch |
HEAD | Current commit you are looking at |
@{u} | Configured upstream tracking branch |
fetch vs pull
git fetch origin # download objects, change nothing local
git diff main origin/main # now you can inspect before integrating
git merge origin/main # integrate deliberately
git pull --rebase # fetch + replay your commits on top💡
git pull is simply fetch followed by merge. Fetching first and inspecting is the safer habit, especially on shared branches.Pushing
git push -u origin feature/search # first time: set upstream
git push # afterwards
git push --force-with-lease # safer than --force
git push origin --delete old-branch⚠️
Prefer
--force-with-lease over --force: it refuses to push if someone else has pushed commits you never fetched, rather than silently discarding them.Keeping branches tidy
git branch -u origin/main # set tracking
git fetch --prune # drop stale remote refs
git branch -vv # see tracking relationshipsFAQ
Why does push say everything is up to date but nothing appears?
You likely committed on a branch with no upstream branch. Use
git push -u origin <branch> once.How do I sync a fork?
Add the upstream remote,
git fetch upstream, then merge or rebase onto upstream/main.Related
Git basics Conflicts, stash and cherry-pick
Last refreshed 2026-09-17.