Sub topic 8
Remote Branches and Pull Requests
This chapter assumes you can run add, commit, push, and pull, and that you have a repository on a host such as GitHub, GitLab, or Bitbucket. It covers how your local branches relate to the remote, and how a pull request turns a pushed branch into a reviewed change.
Remote-tracking branches
When you clone a repository, Git stores a copy of each remote branch under origin/. These are remote-tracking branches, such as origin/main. They are pointers like any other branch, but you cannot commit to them directly. Git moves them only when you talk to the remote with fetch, pull, or push.
This means origin/main shows where main was the last time you fetched, not where it is now. A common error is comparing against a stale origin/main. Run git fetch first.
| Command | What it does |
|---|---|
| `git fetch origin` | Updates `origin/*` pointers. Does not touch your local branches. |
| `git pull` | Runs `fetch`, then merges (or rebases, if configured) the upstream branch into your current branch. |
| `git push` | Sends your commits and moves the remote branch to match. |
Publishing a branch
A new local branch has no remote counterpart. Push it once with -u to create the remote branch and set its upstream, which is the remote branch your local branch tracks.
git switch -c feature/login-form
git commit -am "Add login form"
git push -u origin feature/login-formAfter this, plain git push and git pull on that branch know where to go. git status also reports whether you are ahead of or behind the upstream.
The pull request
A pull request (PR) is a feature of the hosting service, not of Git. It asks to merge one branch (the head) into another (the base), and it gives reviewers a place to comment, run checks, and approve. GitLab calls the same thing a merge request.
Pushing more commits to the same branch updates the open PR automatically. You do not open a new one.
Keeping the branch current
While your PR is open, the base branch keeps moving. Bring your branch up to date by fetching and rebasing, then update the remote. Because rebasing rewrites commits, a normal push will be rejected. Use --force-with-lease, which refuses to overwrite commits you have not fetched.
git switch feature/login-form
git fetch origin
git rebase origin/main
git push --force-with-leaseOnly rewrite a branch that you alone use. If a teammate has pushed to it, a force push can discard their work, even with the lease check.
Common errors
- Rejected push, "non-fast-forward". The remote has commits you lack. Fetch, then merge or rebase, then push.
- PR shows unrelated commits. The base branch is wrong, or your branch was created from an outdated or different branch. Check the base in the PR header.
- Branch deleted on the remote but still listed locally. Run
git fetch --pruneto remove staleorigin/*pointers. - Reviewing old code. Reviewers see what you pushed, not your working directory. Push before asking for review.
Exercise
You rebased your feature branch onto origin/main after it was already pushed, and git push is rejected. What should you run, and why not plain --force?
Show answer
Run git push --force-with-lease. The rebase rewrote your commits, so the remote branch is no longer an ancestor of your local one and a normal push fails. --force-with-lease overwrites the remote only if it still matches what you last fetched, so it will not silently destroy a teammate's newer commits the way --force can.
Exercise
After a colleague merges their PR and deletes the branch on GitHub, git branch -r still lists it. How do you clean it up?
Show answer
Run git fetch --prune. Remote-tracking branches only change when you fetch, and pruning removes the ones whose remote branch no longer exists.