Photo by PJH on Unsplash

On this page

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

Terminal window
git clone https://github.com/org/project.git
cd project

git 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:

Terminal window
git clone /srv/git/project.git my-checkout

Adding and inspecting remotes

For a project you git init locally, create the empty repo on the host, then:

Terminal window
git remote add origin https://github.com/you/project.git
git remote -v
git 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:

Terminal window
git ls-remote origin
git fetch origin
git branch -a

Fetch 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:

Terminal window
git pull origin main
git pull --rebase origin main

Prefer fetch first when you want to see incoming changes before integrating them.

Push and publish a branch

Terminal window
git push
git push origin feature/login
git push -u origin feature/login

Push 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:

Terminal window
git fetch origin feature/login
git switch --track origin/feature/login

Connecting to GitHub step by step

After local commits exist:

Terminal window
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/you/repo.git
git remote -v
git push -u origin main

Use 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.

Search articles

Type a word to search published articles.

Open the search page