skip to content

questions

4

In Git, what does git remote add origin <url> actually change in your repository?

level: juniorimportance: must knowfreq 55%

answer

  1. nothing goes over the wire
  2. it edits one file
  3. name plus URL plus refspec
  4. origin is only a default name
  5. objects arrive at fetch time

basics

~20 s

It only edits .git/config, adding a remote section with a url and a default fetch refspec. No network call happens and no objects arrive until you run git fetch, and the name origin is purely local.

solid answer

~40 s

A remote in Git is nothing but a local nickname for a URL plus a refspec. `git remote add origin <url>` writes a `[remote "origin"]` section into `.git/config` containing `url = <url>` and `fetch = +refs/heads/*:refs/remotes/origin/*`. It contacts nothing and downloads nothing — objects and remote-tracking refs appear only when you run `git fetch`. The name `origin` has no special meaning to Git; it is simply the default name `git clone` picks, and `git remote rename` can change it. `git remote -v` lists each remote's fetch and push URLs, which can differ because `git remote set-url --push` sets the push URL separately. You can add as many remotes as you like; each gets its own namespace under `refs/remotes/<name>/`.

code

ini · 6 lines
ini
[remote "origin"]
	url = https://example.com/team/app.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[remote "upstream"]
	url = https://example.com/vendor/app.git
	fetch = +refs/heads/*:refs/remotes/upstream/*

go deeper

for a junior

Know that a remote is just a name for a URL stored in the repository config, that origin is only a conventional default, and that git remote -v lists what is configured.

for a middle

Explain the full config entry, including the default fetch refspec, and that nothing crosses the network until a fetch or push. Know set-url, rename and remove and what each touches.

for a senior

Show you can debug remote configuration in real situations: repointing after a move, a separate push URL, several remotes with independent namespaces, and cleaning up when a remote is retired.

for a principal

Be ready to reason about how remote naming conventions across many repositories affect automation and onboarding, and where the source of truth for URLs should live rather than being hand-edited per clone.

## A remote is a config entry Newcomers imagine a remote as a connection or a copy of the server. It is neither. A remote is three pieces of local configuration: a name, one or more URLs, and one or more refspecs. `git remote add origin <url>` appends exactly that to `.git/config`, a `[remote "origin"]` section holding `url` and `fetch = +refs/heads/*:refs/remotes/origin/*`. No network traffic occurs. If the URL is wrong you find out at the first `git fetch` or `git push`, not at `git remote add`. ## What the name buys you The name is a local alias so you can type `git fetch origin` instead of the URL. Git attaches no meaning to `origin` — it is merely what `git clone` names the source it cloned from. In a fork setup people conventionally add a second remote called `upstream`, but the names are yours to choose. `git remote rename <old> <new>` renames the config section, moves the remote-tracking refs from `refs/remotes/<old>/` to `refs/remotes/<new>/`, and updates the `branch.<name>.remote` settings that pointed at it. ## Several remotes at once Because each remote's fetch refspec writes into its own `refs/remotes/<name>/` namespace, remotes never collide. With `origin` and `upstream` configured you get both `origin/main` and `upstream/main`, two independent pointers you can compare and merge from. `git fetch --all` updates every configured remote. That is the whole mechanism behind fork workflows: two names, two URLs, two namespaces. ## Inspecting and editing remotes - `git remote -v` lists remotes with their URLs, printing two lines per remote — one tagged `(fetch)` and one `(push)`. - `git remote show <name>` contacts the remote and reports its branches, which ones are tracked, and which local refs are stale. - `git remote get-url <name>` prints the configured URL. - `git remote set-url <name> <newurl>` repoints a remote, for example after a repository moves; `git remote set-url --push <name> <url>` sets a separate push destination, which is what makes the fetch and push lines differ. - `git remote remove <name>` (also spelled `git remote rm`) deletes the config section and the remote-tracking refs under that name. Your local branches are untouched, though ones that tracked the removed remote lose their upstream setting. ## Useful options at add time `git remote add` takes options that pre-shape the configuration: `-f` fetches immediately after adding, `-t <branch>` narrows the fetch refspec to a single branch and may be repeated, `--no-tags` records `tagOpt = --no-tags` so tags are not fetched automatically, and `--mirror=fetch` or `--mirror=push` set up mirroring semantics. Everything they do is expressible as plain config, because that is all a remote is. ## Why interviewers ask The question separates people who treat Git commands as incantations from those who know where state lives. If you can say that it appends a section to `.git/config` and that nothing else happens until a fetch, you can also debug the common failures: pushing to the wrong URL after a repository move, a remote whose refspec was hand-edited, or a `git remote add` that appeared to succeed against a URL that does not exist. ## What it is not Adding a remote does not create a branch, does not set an upstream for any of your branches, and does not authenticate. Those are separate concerns handled at fetch and push time.

  • What happens to your local branches if you run git remote remove origin?
    The config section and all refs under refs/remotes/origin/ are deleted, so origin/main and its siblings disappear. Local branches and their commits are untouched, but any branch that tracked that remote loses its upstream, so git status stops reporting ahead and behind counts until you configure a new one.
  • Why does git remote -v print two lines per remote?
    Because a remote can fetch from one URL and push to another. The lines are tagged (fetch) and (push); they are identical unless a push URL was set with git remote set-url --push, which records a separate pushurl entry in the configuration.
  • How does Git keep two remotes from clashing when both have a branch called main?
    Each remote's fetch refspec writes into its own namespace, so one lands in refs/remotes/origin/main and the other in refs/remotes/upstream/main. They are separate refs you can diff or merge independently; nothing is overwritten because the destination side of each refspec differs.

saying these in an interview costs you the question

  • Thinks git remote add contacts the server or downloads objects
  • Believes the name origin is special to Git
  • Says a remote is a copy of the remote repository
  • Assumes adding a remote sets an upstream for the current branch
  • Cannot say where remote configuration is stored

context

open as a page

What does the refspec +refs/heads/*:refs/remotes/origin/* mean in Git?

level: middleimportance: must knowfreq 42%

basics

~20 s

It is the default fetch rule: take every branch under refs/heads/ on the remote and store it locally under refs/remotes/origin/, with the leading plus allowing those local copies to be updated even when the change is not a fast-forward.

open as a page

In Git, what is a stale remote-tracking branch and what does git remote prune do?

level: middleimportance: should knowfreq 28%

basics

~10 s

A stale remote-tracking ref is one like origin/feature whose branch no longer exists on the remote; fetching never deletes it. git remote prune origin removes those refs, leaving your local branches and commits untouched.

open as a page

What does git clone --mirror give you that a plain bare clone does not?

level: seniorimportance: should knowfreq 20%

basics

~20 s

A mirror clone is bare and additionally maps every ref with +refs/:refs/, recording remote.origin.mirror, so an update overwrites all local refs with the remote's. A plain bare clone copies branch heads once and sets up no such tracking.

open as a page