Work with remotes, fetch, pull and push
Work with remotes, fetch, pull and push
A remote is simply another Git repository that your local repository knows by a short name such as origin. GitHub can host a remote, but the remote concept belongs to Git itself.
What you’ll learn
- Add and inspect remotes.
- Explain remote-tracking branches.
- Distinguish fetch, pull and push.
Local and remote references are separate
Remote
Another repository has its own main.
remote mainFetch
Update local knowledge of that branch.
origin/mainIntegrate / push
Merge/rebase locally or send local commits.
synchronized history
Fetching updates your knowledge; merging changes your branch
git fetch origin downloads objects and updates remote-tracking references such as origin/main; it does not automatically merge them into your checked-out branch.
git pull is a convenience operation that fetches and then integrates according to configuration/options. git push sends local objects and asks the remote to update a reference. A push can be rejected when the remote branch has commits your local branch does not contain.
Synchronize deliberately
Run this in a disposable practice repository unless the lesson explicitly says otherwise.
git remote -v
git fetch origin
git log --oneline --graph --decorate --all
git pull --ff-only
git push origin mainFetching first lets you inspect remote changes before integrating them. --ff-only makes pull refuse rather than create a merge when the update is not a fast-forward.
Practice: Remotes, fetch, pull and push
Your task: Using two clones of a disposable repository, commit from clone A and push. In clone B, run fetch and inspect origin/main before changing local main.
Common pitfall to avoid
Avoid reflexively using force push. If rewriting a branch is intentional and agreed, --force-with-lease is usually safer than unconditional --force because it checks the remote state you expect.
Check your understanding
Remotes, fetch, pull and push: what to remember
Work with remotes, fetch, pull and push is complete when you can explain the state being changed before you run the command, not merely repeat the syntax.
Read further: official supporting documentation.
Reaching the recap and completing the quizzes are separate milestones.