Skip to content
$ lynuxpro
← Git

LESSON 9 OF 21 · INTERMEDIATE · 20 MIN

Working with remotes

HOW IT WORKSclone, fetch, pull and push

cloneorigingitlab.com/acme/shoporigin/mainremote-trackingmainyour branchWorking treefiles
1/4

git clone copies the whole repository and remembers where it came from as origin.

$ git clone https://gitlab.com/acme/shop.git

So far everything happened on your machine. Real teams share work through a remote: a copy of the repository on a server such as GitLab or GitHub. This lesson covers cloning, how remote-tracking branches work, the difference between fetch and pull, pushing branches for merge requests, and what to do when a push is rejected because someone else pushed first.

In simple words

  • Remote: a copy of the repository on a server, such as GitLab or GitHub. Usually called origin.
  • Clone: downloading a full copy of a remote repository.
  • Push: sending your commits to the remote.
  • Fetch vs pull: fetch downloads new commits without changing your files. Pull fetches and then merges them in.
  • Protected branch: a branch on the server that cannot be pushed to directly or deleted. Changes must go through a merge request.

What a remote is

A remote is a named URL pointing to another copy of the repository. When you clone, Git creates one called origin automatically.

GitLab: origin/main ──── fetch ───► your origin/main ──── merge/rebase ───► your main        ◄──────────────────────────────── push ──────────────────────────────────
OUTPUTterminalutf-8 · 2L

Your repository keeps remote-tracking branches like origin/main: read-only bookmarks recording where the server's branches were the last time you talked to it. They only update when you fetch, pull or push.

Clone

git config --global user.name "Leela Kumar"git clone https://gitlab.com/acme/shop.gitcd shopgit remote -vgit branch -a
SHELLbashutf-8 · 5L
Cloning into 'shop'...remote: Enumerating objects: 36, done.remote: Counting objects: 100% (36/36), done.remote: Compressing objects: 100% (27/27), done.Receiving objects: 100% (36/36), 4.21 KiB | 4.21 MiB/s, done.Resolving deltas: 100% (9/9), done.origin	https://gitlab.com/acme/shop.git (fetch)origin	https://gitlab.com/acme/shop.git (push)* main  remotes/origin/main
OUTPUTterminalutf-8 · 10L

You got the full history, a local main that tracks origin/main, and the remote named origin. Two URL styles exist:

  • HTTPS: https://gitlab.com/acme/shop.git (authenticate with a token)
  • SSH: git@gitlab.com:acme/shop.git (authenticate with your SSH key)

Useful clone options: --depth 1 (only the latest commit, much faster for CI), -b release/2.4 (check out a specific branch) and a folder name as the last argument.

Push a branch and open a merge request

The normal team workflow is: create a branch, commit, push the branch, open a merge request (MR) for review.

git switch -c fix/gst-rateprintf 'TAX_RATE = 0.18\n\n\ndef total(subtotal):\n    return round(subtotal * (1 + TAX_RATE), 2)\n' > pricing.pygit commit -am "Fix GST rate back to 18%"git push -u origin fix/gst-rate
SHELLbashutf-8 · 4L
[fix/gst-rate 3c9d2e1] Fix GST rate back to 18% 1 file changed, 1 insertion(+), 1 deletion(-)Enumerating objects: 3, done.Counting objects: 100% (3/3), done.Writing objects: 100% (3/3), 1.02 KiB | 1.02 MiB/s, done.Total 3 (delta 1), reused 0 (delta 0), pack-reused 0remote: remote: To create a merge request for fix/gst-rate, visit:remote:   https://gitlab.com/acme/shop/-/merge_requests/new?merge_request%5Bsource_branch%5D=fix%2Fgst-rateremote: To https://gitlab.com/acme/shop.git * [new branch]      fix/gst-rate -> fix/gst-ratebranch 'fix/gst-rate' set up to track 'origin/fix/gst-rate'.
OUTPUTterminalutf-8 · 13L
  • -u (--set-upstream) links your local branch to the remote one, so afterwards a plain git push or git pull knows where to go.
  • GitLab prints a link to open a merge request for the new branch.

Check the tracking relationship and whether you are ahead or behind:

git branch -vvgit status
SHELLbashutf-8 · 2L
* fix/gst-rate    3c9d2e1 [origin/fix/gst-rate] Fix GST rate back to 18%  main            ce797e5 [origin/main] Add CI pipelineOn branch fix/gst-rateYour branch is up to date with 'origin/fix/gst-rate'.nothing to commit, working tree clean
OUTPUTterminalutf-8 · 6L

Fetch versus pull

  • git fetch downloads new commits and updates origin/* branches. It never changes your branches or files. Always safe.
  • git pull is git fetch followed by integrating origin/<branch> into your current branch, by merge (default) or rebase (--rebase, or pull.rebase true in your config).

Fetching first lets you look before you integrate:

git fetchgit log --oneline main..origin/main     # what is new on the servergit diff main origin/main               # what changedgit pull --rebase                       # then bring it in
SHELLbashutf-8 · 4L

When a push is rejected

Simulate a teammate. Clone the repository a second time in another folder (that clone plays your colleague Ravi), commit there and push to main:

cd ~git clone https://gitlab.com/acme/shop.git ravi-shopcd ravi-shopprintf 'flask==3.1.0\ngunicorn==23.0.0\nrequests==2.32.3\n' > requirements.txtgit commit -am "Add requests"git push
SHELLbashutf-8 · 6L

Now go back to your copy, make a commit on main and try to push:

cd ~/shopgit switch mainprintf '# Shop\n\nCheckout service for acme.\n' > README.mdgit commit -am "Rewrite README intro"git push
SHELLbashutf-8 · 5L
To https://gitlab.com/acme/shop.git ! [rejected]        main -> main (fetch first)error: failed to push some refs to 'https://gitlab.com/acme/shop.git'hint: Updates were rejected because the remote contains work that you do nothint: have locally. This is usually caused by another repository pushing tohint: the same ref. If you want to integrate the remote changes, usehint: 'git pull' before pushing again.
OUTPUTterminalutf-8 · 7L

Git refuses to overwrite Ravi's commit. Integrate it, then push again:

git pull --rebasegit log --oneline -n 3git push
SHELLbashutf-8 · 3L
From https://gitlab.com/acme/shop.git   ce797e5..8b1a9c3  main       -> origin/mainSuccessfully rebased and updated refs/heads/main.2f7e6d5 (HEAD -> main) Rewrite README intro8b1a9c3 (origin/main) Add requestsce797e5 Add CI pipeline
OUTPUTterminalutf-8 · 6L

Your commit was replayed on top of Ravi's, giving a straight history, and the push now succeeds.

Never "fix" a rejected push with git push --force on a shared branch. It deletes your teammates' commits from the server. If you must force-push your own feature branch after a rebase, use git push --force-with-lease, which refuses if someone else pushed in the meantime.

Managing remotes

git remote -v                                  # listgit remote add upstream https://gitlab.com/acme/shop.gitgit remote set-url origin git@gitlab.com:leela/shop.gitgit remote remove upstreamgit fetch --prune                              # drop origin/* branches deleted on the servergit push origin --delete fix/gst-rate          # delete a remote branch
SHELLbashutf-8 · 6L

A second remote called upstream is the standard setup when you work on a fork: origin is your fork (you push there) and upstream is the original project (you fetch from there and open merge requests to it).

Protected branches

On GitLab and GitHub, main is usually protected: nobody can push to it directly, force pushes are blocked, and changes arrive only through merge requests that pass the pipeline and get approved. That is why the everyday workflow is "branch, push, MR" rather than pushing to main.

Real-world scenario: the force push that erased a morning

A developer rebased their local main, got a rejected push, and ran git push --force. Three colleagues' commits from that morning disappeared from the server. Recovery was possible only because one colleague still had the commits locally and pushed them back. Afterwards the team protected main on GitLab (no direct pushes, no force pushes) and added pull.rebase true to everyone's config so git pull would rebase local work cleanly instead.

Common mistakes

  • Forgetting -u on the first push, then wondering why git pull asks which branch to use.
  • git push --force on shared branches. Use --force-with-lease, and only on your own branches.
  • Working on stale code: run git pull (or fetch) before starting new work.
  • Thinking origin/main updates by itself: it only changes when you fetch, pull or push.
  • Committing directly to main when the team uses merge requests.

Practice

  1. Clone https://gitlab.com/acme/shop.git and list remotes and branches with git remote -v and git branch -a.
  2. Create a branch, commit a change and push it with -u. Check git branch -vv.
  3. Reproduce a rejected push with a second clone, then fix it with git pull --rebase and push.
  4. Run git fetch and compare main with origin/main using git log --oneline main..origin/main.

Cheat sheet

git clone URL [dir] [--depth 1] [-b branch]git remote -v | add name URL | set-url | removegit branch -a | -vv                  remote and tracking infogit fetch [--prune]                  download, change nothinggit pull [--rebase]                  fetch + integrategit push -u origin branch            first push of a branchgit push                             later pushesgit push --force-with-lease          safe force (own branches only)git push origin --delete branch      delete remote branchgit log --oneline main..origin/main  what is new on the server
OUTPUTterminalutf-8 · 10L

Key takeaways

  • A remote is a named URL; origin is created by git clone.
  • origin/main is a local bookmark of the server's branch, updated by fetch, pull and push.
  • fetch is always safe; pull is fetch plus merge or rebase.
  • Push feature branches with -u and open merge requests; main is usually protected.
  • A rejected push means someone pushed first: pull (preferably with --rebase), then push. Never force-push shared branches.