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 ──────────────────────────────────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 -aCloning 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/mainYou 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[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'.-u(--set-upstream) links your local branch to the remote one, so afterwards a plaingit pushorgit pullknows 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* 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 cleanFetch versus pull
git fetchdownloads new commits and updatesorigin/*branches. It never changes your branches or files. Always safe.git pullisgit fetchfollowed by integratingorigin/<branch>into your current branch, by merge (default) or rebase (--rebase, orpull.rebase truein 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 inWhen 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 pushNow 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 pushTo 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.Git refuses to overwrite Ravi's commit. Integrate it, then push again:
git pull --rebasegit log --oneline -n 3git pushFrom 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 pipelineYour 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 branchA 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
-uon the first push, then wondering whygit pullasks which branch to use. git push --forceon 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/mainupdates by itself: it only changes when you fetch, pull or push. - Committing directly to
mainwhen the team uses merge requests.
Practice
- Clone
https://gitlab.com/acme/shop.gitand list remotes and branches withgit remote -vandgit branch -a. - Create a branch, commit a change and push it with
-u. Checkgit branch -vv. - Reproduce a rejected push with a second clone, then fix it with
git pull --rebaseand push. - Run
git fetchand comparemainwithorigin/mainusinggit 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 serverKey takeaways
- A remote is a named URL;
originis created bygit clone. origin/mainis a local bookmark of the server's branch, updated by fetch, pull and push.fetchis always safe;pullis fetch plus merge or rebase.- Push feature branches with
-uand open merge requests;mainis usually protected. - A rejected push means someone pushed first: pull (preferably with
--rebase), then push. Never force-push shared branches.