ProgramGuru

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

  1. Remote

    Another repository has its own main.

    remote main
  2. Fetch

    Update local knowledge of that branch.

    origin/main
  3. Integrate / push

    Merge/rebase locally or send local commits.

    synchronized history
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 main

Fetching 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

What does git fetch normally do to your checked-out local branch?
Choose one answer. You can try again.

Answer and explanation

It downloads remote updates but does not automatically merge them into the branch.

Correct.

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.

    Next lesson: Rebase local work and clean up history →