Git and GitHub / Basic: the local Git model / Lesson 6
Remotes, fetch, and tracking references
Understand local knowledge of another repository before choosing how to integrate or publish changes.
On this page
Lesson goals
- Distinguish a remote name, remote-tracking ref, and upstream branch.
- Inspect divergence and choose an explicit integration policy.
A remote is configuration for another repository
A remote name such as origin is a local nickname associated with URLs and fetch rules. It is not a special central authority built into Git. A clone commonly creates origin, but names and defaults can be changed. Remote-tracking references such as origin/main are local records of observed remote state, usually updated by fetch. They are not live views that change whenever a collaborator commits. An upstream relationship tells commands which branch to compare or integrate with by default; it does not grant access or guarantee that the remote branch still exists.
Inspect fetched knowledge without moving your branch
Fetching normally downloads objects and updates selected tracking references while leaving the current branch and working files unchanged. That separation lets you inspect new history before integrating it. In this course, any fetch validation uses a separate local filesystem repository inside temporary storage, never a hosted service or an actual configured remote. The fragment below is explanatory and non-runnable because origin is context-dependent. Before real network use, confirm the intended repository, authentication scope, and project policy without exposing credentials. A URL containing a token is not a safe teaching or logging format.
git fetch origin
git log --oneline --left-right main...origin/main
git diff --stat main origin/main
Only a separately configured local lab origin may be used during practice. The log shows commits unique to either side, and the diff compares tip content. No fixed output is promised because the result depends on the two histories. No push command is executed in this course.
Read ahead and behind as reachability counts
Ahead and behind counts describe commits reachable from one selected tip but not the other. They do not measure lines changed, risk, or the number of collaborators’ edits. They also depend on how fresh the tracking reference is. A local branch can be ahead and behind simultaneously after independent progress. The read-only count below uses local refs after the lab has fetched a local origin. Interpret each number against the documented argument order, then inspect the commits. Do not resolve divergence by overwriting remote history merely because a status summary looks inconvenient.
git rev-list --left-right --count main...origin/main
git branch -vv
For a lab with one unique local commit and two unique commits in origin/main, the count is 1 then 2, separated by whitespace. branch -vv adds current reference and upstream information; its generated ids and freshness depend on the lab.
Choose integration instead of relying on defaults
git pull combines fetch with an integration step chosen by options and configuration. Depending on explicit policy, this can involve fast-forward-only behavior, merging, or rebasing. Defaults and refusal messages vary with Git version and configuration, so do not teach pull as universally equivalent to one fixed sequence. Fetch then inspect is a clear learning workflow. If the project uses pull --ff-only, divergence should stop for a decision. If it uses rebase for private topics, understand that replay creates new commit identities. Shared-history conventions remain a collaboration agreement rather than a local convenience.
Treat publication as a separate authorized action
Push asks another repository to update references and may be rejected by ancestry, permissions, protected branches, rulesets, or server checks. Authentication success does not imply authorization to change every branch. A non-fast-forward rejection often protects commits absent from your proposed history. Preserve and integrate collaborators’ work through the agreed workflow before attempting publication. Force options are not routine synchronization tools, and lease checks reduce only certain overwrite races rather than replacing coordination. This course explains publication behavior without sending any push, changing actual remotes, or using real credentials.
Common errors and fixes
origin/main is mistaken for current server state
- Why it happens
- A local tracking ref is treated as a live view.
- How to fix it
- Refresh through the authorized fetch workflow and inspect again.
- Prevent it next time
- Record when remote knowledge was obtained.
Pull unexpectedly rewrites a private topic
- Why it happens
- Rebase behavior came from options or configuration.
- How to fix it
- Inspect the integration policy and preserve recovery references.
- Prevent it next time
- Use explicit integration choices while learning.
A push is rejected despite valid login
- Why it happens
- Ancestry or server policy disallows the update.
- How to fix it
- Read the exact rejection and inspect the intended branch relationship.
- Prevent it next time
- Separate authentication, authorization, and graph safety.
Check your understanding
1. A branch is ahead one and behind two. What does that establish?
Show worked solution
Each tip has unique reachable commits relative to the compared refs. Inspect those commits and tracking freshness, then choose the agreed merge or private-history rebase workflow. The counts do not justify discarding the remote side.
2. Fetching succeeds. Has the working branch integrated the new commits?
Show worked solution
Not ordinarily. Fetch updates fetched objects and tracking knowledge, while the current local branch usually remains unchanged. Verify the branch tip and explicitly integrate according to policy. Do not confuse updated remote-tracking history with a completed merge.
Key takeaways
- Remote-tracking refs are local observations.
- Fetch, integration, and publication are separate decisions.
- Protect collaborators’ history and follow server policy.