Git remotes: push, pull and fetch explained
A Git remote is a named URL of another copy of the repository, and you send commits to it with git push and bring new ones back with git fetch or git pull.
- Updated Nov 27, 2026
- Markdown
Add a remote, push a branch with tracking, tell fetch from pull, and fix a rejected non-fast-forward push without losing work.
- A local repository with commits
- An empty repository on a Git host such as GitHub, GitLab or Gitea
A remote connects your laptop to your server
- git push uploads your new commits to the remote called origin.
- git fetch downloads the new commits from the remote without changing your files.
- git pull fetches and then merges the new commits into the server checkout.
- You trigger a deploy, for example over SSH, so the server restarts the app with the new code.
Static diagram
- Your laptopgit pushRemote origin
- Server clonegit fetchRemote origin
- Remote origingit pullServer clone
- Your laptopdeployServer clone
A remote is a name for the URL of another copy of your repository, usually on GitHub, GitLab or your own server. git push sends your commits there, git fetch downloads commits you do not have, and git pull does a fetch plus a merge. Most daily trouble comes from not knowing which of the two copies has the newer work.
How it works#
Your repository is complete on its own. A remote is just a bookmark. The default bookmark is called origin, and git clone creates it for you. Run this to see yours:
git remote -vIt lists each remote with a fetch URL and a push URL, such as origin git@example.com:team/app.git (fetch).
Git also keeps remote-tracking branches, like origin/main. They are your last known copy of the remote’s branches. They only change when you fetch, push or pull. A local branch can track one: main tracks origin/main, and git status uses that link to say Your branch is ahead of 'origin/main' by 2 commits.
Step 1: Add a remote#
Create an empty repository on your host first, without a README. Then:
git remote add origin git@example.com:team/app.git
git remote -vYou can use an HTTPS URL or an SSH URL. SSH needs a key, which the SSH keys lesson covers. To change the URL later, use git remote set-url origin <new-url>.
Step 2: Push with tracking#
git push -u origin main-u (short for --set-upstream) links your local main to origin/main. After this, git push and git pull with no arguments work on that branch. You only need -u the first time for each branch. To push a new feature branch, run git push -u origin feature-health.
Step 3: Fetch, then pull#
git fetch origin
git log --oneline main..origin/main # commits on the remote you do not have
git merge origin/main # integrate themgit fetch is read-only for your working files, so it is always safe. git pull combines the last two steps:
git pullBy default it merges. With pull.rebase true it replays your commits on top instead, and with pull.ff only it refuses anything but a fast-forward, as set in the configuration lesson. Many people prefer fetching first and looking, especially on production servers.
Step 4: Handle a rejected push#
Someone else pushed to main before you:
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'git@example.com:team/app.git'
hint: Updates were rejected because the tip of your current branch is behindGit is protecting their commits. Fix it like this:
git fetch origin
git merge origin/main # or: git rebase origin/main
git pushIf the merge stops, see resolving conflicts.
Force pushing, with care#
Sometimes you really did rewrite your own commits, for example after an amend or a rebase of a private branch. Then Git rejects the push and you may be tempted by --force.
Warning: git push --force overwrites the remote branch with your version and can destroy other people’s commits. Never use it on main or a shared branch. If you must force push your own branch, use:
git push --force-with-leaseIt refuses when the remote branch has commits you have not seen. It is still a rewrite, so tell your team first, and check that branch protection on the host does not forbid it.
Pushes over SSH use an SSH key, and a server pulling code over SSH is how most self-hosted deploys start. The blog post Deploy an Express app with Docker on a VPS shows a server pulling code from a remote, and the workflow lesson shows when pushes trigger deploys.
Check it worked#
git status -sb
git branch -vvgit status -sb prints a first line such as ## main...origin/main, with [ahead 1] or [behind 2] when the two differ. git branch -vv shows each branch with its upstream and the last commit. After a successful push, both should show no difference. You should also see your commits on the host’s web page.
Edge cases and gotchas#
- Fetch before you judge.
git statuscompares to your last fetched copy. It can sayup to datewhile the remote has moved on. - Tags are not pushed by default. Use
git push origin v1.2.0orgit push --tags, covered in the deploy workflow lesson. - Deleting a remote branch.
git push origin --delete feature-healthremoves it on the remote. Your local branch remains until you delete it. - Stale remote branches.
git fetch --pruneremoves remote-tracking branches that no longer exist on the remote. - Multiple remotes. You can add more than one, such as
upstreamfor the project you forked. Name them clearly. - Credentials. Do not put a token in the remote URL. It ends up in
.git/configin plain text.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
fatal: 'origin' does not appear to be a git repository | The remote name is wrong or missing | git remote -v, then git remote add origin <url> |
error: remote origin already exists. | You ran add twice | git remote set-url origin <url> |
fatal: The current branch feature has no upstream branch. | No tracking set | git push -u origin feature |
! [rejected] main -> main (non-fast-forward) | The remote has commits you lack | Fetch, merge or rebase, push again |
Permission denied (publickey). | No usable SSH key for this host | Add the key, see the SSH keys lesson |
fatal: Could not read from remote repository. | Wrong URL, no access or no network | Check the URL and your access rights |
remote: Permission to team/app.git denied to deploy-key. | A read-only key tried to push | Use a key with write access on a machine that should push |
fatal: refusing to merge unrelated histories | The remote already had its own first commit | Pull with --allow-unrelated-histories once, or recreate the remote empty |
fatal: unable to access 'https://...': Could not resolve host | DNS or network problem | Check connectivity with ping and DNS settings |
Quick reference#
git remote -v
git remote add origin git@example.com:team/app.git
git push -u origin main
git fetch origin
git pull
git branch -vv
git push --force-with-lease # only for your own rewritten branch
git push origin --delete old-branch
git fetch --pruneRelated lessons#
- Git branches and merging: the branches you now share.
- SSH keys and deploy keys for Git: authenticate pushes and pulls.
- Resolve Git merge conflicts: when a pull cannot merge cleanly.
- Undo mistakes in Git: why amended commits cause rejected pushes.
- What is SSH and how does it work: the transport behind git@ URLs.
Next#
Set up passwordless access in SSH keys and deploy keys for Git.
Check what you learned
Frequently asked questions
What is the difference between git fetch and git pull?
git fetch downloads new commits from the remote and updates your remote-tracking branches, but does not change your files. git pull runs fetch and then merges or rebases the result into your current branch.
What does git push -u origin main do?
It pushes your main branch to the remote called origin and sets origin/main as its upstream. After that, plain git push and git pull know where to go.
What does rejected non-fast-forward mean?
The remote branch has commits that you do not have, so your push would overwrite them. Fetch, integrate the remote commits with merge or rebase, then push again.
Is git push --force safe?
Not on a shared branch, because it overwrites commits that others may have. If you must, use git push --force-with-lease, which refuses when the remote has changed since your last fetch.
Sources
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.