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

URL: https://levelrail.com/learn/git/git-remotes-push-pull-fetch/
Updated: 2026-11-27

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:

```bash
git remote -v
```

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

```bash
git remote add origin git@example.com:team/app.git
git remote -v
```

You can use an HTTPS URL or an SSH URL. SSH needs a key, which the [SSH keys lesson](https://levelrail.com/learn/git/ssh-keys-and-deploy-keys-for-git/)) covers. To change the URL later, use `git remote set-url origin <new-url>`.

## Step 2: Push with tracking

```bash
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

```bash
git fetch origin
git log --oneline main..origin/main   # commits on the remote you do not have
git merge origin/main                 # integrate them
```

`git fetch` is read-only for your working files, so it is always safe. `git pull` combines the last two steps:

```bash
git pull
```

By 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](https://levelrail.com/learn/git/install-and-configure-git/)). Many people prefer fetching first and looking, especially on production servers.

## Step 4: Handle a rejected push

Someone else pushed to `main` before you:

```text
! [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 behind
```

Git is protecting their commits. Fix it like this:

```bash
git fetch origin
git merge origin/main     # or: git rebase origin/main
git push
```

If the merge stops, see [resolving conflicts](https://levelrail.com/learn/git/resolve-git-merge-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:

```bash
git push --force-with-lease
```

It 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](https://levelrail.com/learn/glossary/#ssh-key)), and a server pulling code over [SSH](https://levelrail.com/learn/glossary/#ssh)) is how most self-hosted deploys start. The blog post [Deploy an Express app with Docker on a VPS](https://levelrail.com/blog/deploy-express-app-docker-vps/)) shows a server pulling code from a remote, and the [workflow lesson](https://levelrail.com/learn/git/git-workflow-that-triggers-a-deploy/)) shows when pushes trigger deploys.

## Check it worked

```bash
git status -sb
git branch -vv
```

`git 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 status` compares to your last fetched copy. It can say `up to date` while the remote has moved on.
- **Tags are not pushed by default.** Use `git push origin v1.2.0` or `git push --tags`, covered in the [deploy workflow lesson](https://levelrail.com/learn/git/git-workflow-that-triggers-a-deploy/)).
- **Deleting a remote branch.** `git push origin --delete feature-health` removes it on the remote. Your local branch remains until you delete it.
- **Stale remote branches.** `git fetch --prune` removes remote-tracking branches that no longer exist on the remote.
- **Multiple remotes.** You can add more than one, such as `upstream` for the project you forked. Name them clearly.
- **Credentials.** Do not put a token in the remote URL. It ends up in `.git/config` in 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

```bash
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 --prune
```

## Related lessons

- [Git branches and merging](https://levelrail.com/learn/git/git-branches-and-merging/)): the branches you now share.
- [SSH keys and deploy keys for Git](https://levelrail.com/learn/git/ssh-keys-and-deploy-keys-for-git/)): authenticate pushes and pulls.
- [Resolve Git merge conflicts](https://levelrail.com/learn/git/resolve-git-merge-conflicts/)): when a pull cannot merge cleanly.
- [Undo mistakes in Git](https://levelrail.com/learn/git/undo-mistakes-in-git/)): why amended commits cause rejected pushes.
- [What is SSH and how does it work](https://levelrail.com/learn/server-basics/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](https://levelrail.com/learn/git/ssh-keys-and-deploy-keys-for-git/)).

## 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

- [git-remote manual](https://git-scm.com/docs/git-remote)
- [git-push manual](https://git-scm.com/docs/git-push)
- [git-fetch manual](https://git-scm.com/docs/git-fetch)
- [git-pull manual](https://git-scm.com/docs/git-pull)
