Chapter 6 · Lesson 7 of 10
Git for deployers

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.

By the end you will

Add a remote, push a branch with tracking, tell fetch from pull, and fix a rejected non-fast-forward push without losing work.

Time
15 min
Level
Beginner
Check
4 questions
You need
  • 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

A remote is a shared copy of the repository: you push to it from your laptop and pull from it on the server.
  1. git push uploads your new commits to the remote called origin.
  2. git fetch downloads the new commits from the remote without changing your files.
  3. git pull fetches and then merges the new commits into the server checkout.
  4. You trigger a deploy, for example over SSH, so the server restarts the app with the new code.
Static diagram
  1. Your laptopgit pushRemote origin
  2. Server clonegit fetchRemote origin
  3. Remote origingit pullServer clone
  4. 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:

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 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. Many people prefer fetching first and looking, especially on production servers.

Step 4: Handle a rejected push#

Someone else pushed to main before you:

output
! [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.

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

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

SymptomLikely causeFix
fatal: 'origin' does not appear to be a git repositoryThe remote name is wrong or missinggit remote -v, then git remote add origin <url>
error: remote origin already exists.You ran add twicegit remote set-url origin <url>
fatal: The current branch feature has no upstream branch.No tracking setgit push -u origin feature
! [rejected] main -> main (non-fast-forward)The remote has commits you lackFetch, merge or rebase, push again
Permission denied (publickey).No usable SSH key for this hostAdd the key, see the SSH keys lesson
fatal: Could not read from remote repository.Wrong URL, no access or no networkCheck the URL and your access rights
remote: Permission to team/app.git denied to deploy-key.A read-only key tried to pushUse a key with write access on a machine that should push
fatal: refusing to merge unrelated historiesThe remote already had its own first commitPull with --allow-unrelated-histories once, or recreate the remote empty
fatal: unable to access 'https://...': Could not resolve hostDNS or network problemCheck 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

Next#

Set up passwordless access in SSH keys and deploy keys for Git.

Check what you learned4 questions
1What does origin mean in git push origin main?
2Which command downloads new remote commits without touching your working files?
3Your push is rejected as non-fast-forward. What is the right first move?
4How is --force-with-lease safer than --force?

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

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios