Sub topic 10
Keeping your clone up to date
This chapter assumes you have already cloned a repository, so you have an origin remote and a checked-out branch. It also assumes you know what a commit and a branch are.
Why a clone goes stale
A clone is a snapshot taken when you ran git clone. GitHub does not push later commits to your machine. Your copy changes only when you ask Git to download new work.
Git keeps two kinds of branches in your clone:
- A local branch such as
mainis the one you work on. - A remote-tracking branch such as
origin/mainis Git's record of wheremainwas on GitHub the last time you contacted it.
Updating happens in two steps. First Git downloads new commits and moves origin/main. Then your local main is brought up to match.
Fetch, then decide
git fetch downloads new commits and updates origin/main. It never changes your local branches or your files, so it is always safe to run.
git fetch origin
git status
git log --oneline HEAD..origin/maingit status reports whether your branch is behind origin/main. The git log command lists the commits you do not have yet. If it prints nothing, you are current.
Pull in one step
git pull runs git fetch and then integrates the result into your current branch.
git fetch origin
git pullThe git fetch origin line is optional before a pull, because pull fetches anyway. Running both here shows the two steps.
By default, a pull merges. If you have local commits that origin lacks, Git creates a merge commit. You can ask for a rebase instead, which replays your commits on top of the new ones.
git pull --rebaseCommon errors
| Message or symptom | Cause | Fix |
|---|---|---|
| `Your local changes ... would be overwritten by merge` | Uncommitted edits touch files the update also changes | Commit or `git stash` your changes, then pull again |
| `hint: You have divergent branches` | Both your branch and origin have new commits, and no pull strategy is set | Run `git pull --rebase`, `git pull --no-rebase`, or `git pull --ff-only` |
| `CONFLICT (content)` | The same lines changed on both sides | Edit the marked files, run `git add` on them, then finish with `git commit` (or `git rebase --continue`) |
| `There is no tracking information for the current branch` | Your local branch is not linked to a remote branch | Run `git branch --set-upstream-to=origin/main` |
To stop seeing the divergent-branches hint, set a default once:
git config --global pull.rebase trueUpdating a fork
In a fork, origin is your copy. New work lands in the original repository, which you added as upstream. Fetch from upstream, update your local branch, then push to your fork.
git fetch upstream
git switch main
git merge --ff-only upstream/main
git push origin mainThis assumes the default branch is main. Check with git branch -a if you are unsure.
Shallow clones
If you cloned with --depth 1, fetching still works for new commits. To get the full history later, run git fetch --unshallow.
Exercise
You run git fetch origin and then git status, which says "Your branch is behind 'origin/main' by 3 commits". Your working files have not changed. Why, and which command brings your local branch up to date without creating a merge commit?
Show answer
git fetch only updates the remote-tracking branch origin/main. It does not touch your local branch or files. Run git merge --ff-only origin/main, or git pull --ff-only, to move your branch forward. This works because your branch has no commits of its own.