This chapter follows Everyday Git commands.
A remote is a named URL (or path) where a copy of the project lives—GitHub, GitLab, Bitbucket, or a colleague’s bare repo on disk. Your local repository holds the full object database; remotes are bookmarks plus refs you synchronize with fetch, pull, and push.
Clone and the default remote
git clone https://github.com/org/project.gitcd projectgit clone downloads objects, checks out a working tree, and creates origin pointing at the URL. It usually creates a local branch that tracks origin/main (or origin/master on older repos).
Clone from a local path the same way:
git clone /srv/git/project.git my-checkoutAdding and inspecting remotes
For a project you git init locally, create the empty repo on the host, then:
git remote add origin https://github.com/you/project.gitgit remote -vgit push -u origin main-u sets upstream tracking so later git push and git pull know which remote branch to use.
List remote branches without checking them out:
git ls-remote origingit fetch origingit branch -aFetch versus pull
git fetch downloads commits and updates remote-tracking refs (for example origin/main) without changing your current branch or working tree. You inspect with git log origin/main, git diff main origin/main, then merge or rebase locally.
git pull is fetch plus a merge (or rebase if configured) into your current branch:
git pull origin maingit pull --rebase origin mainPrefer fetch first when you want to see incoming changes before integrating them.
Push and publish a branch
git pushgit push origin feature/logingit push -u origin feature/loginPush fails if the remote has commits you lack—someone else pushed first. Integrate their work (pull, merge, or rebase), then push again.
Track a remote branch locally:
git fetch origin feature/logingit switch --track origin/feature/loginConnecting to GitHub step by step
After local commits exist:
git initgit add .git commit -m "Initial commit"git remote add origin https://github.com/you/repo.gitgit remote -vgit push -u origin mainUse SSH URLs if your keys are configured: git@github.com:you/repo.git.
Pitfall: pull with dirty working tree
| User A | User A working tree | User B working tree | User B |
|---|---|---|---|
Has text.txt |
Init content | Init content | Has text.txt |
| Edits, commits, pushes | |||
| Init content | Edits without pulling | ||
git pull → commit or stash first |
|||
| Commits, pulls → merge conflict | |||
| Resolves, commits, pushes |
Pull only when the working tree is clean—changes committed, stashed, or discarded. Otherwise Git may refuse the merge or produce a messy conflict. Commit early, pull often when collaborating on one branch.
Pitfall: force push
| User A | User A tree | User B tree | User B |
|---|---|---|---|
Both start with demo1.txt |
Init | Init | |
Adds demo2.txt, pushes |
|||
| A commits locally, push rejected | |||
A runs git push --force |
B’s commit disappears from remote |
--force rewrites the remote branch to match your local history. Teammates’ commits can vanish from the shared branch. Prefer git push --force-with-lease, which fails if the remote moved since your last fetch—safer, not risk-free.
Never force-push shared main unless your team explicitly allows it (and usually not even then).
What remotes do not store
Remotes hold Git objects and refs. Issues, pull request threads, and CI configuration live on the host. Your .git directory is portable; platform features are not.
Next: Undoing and moving changes.