Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Git

    Remotes: Clone, Fetch, Pull, Push

    A remote is another copy of the repository, usually on GitHub. The default name for the one you cloned from is

    Chapter 7 of 114 min read

    The Plain Picture#

    A shared album in the school library. Everyone keeps their own full copy at home (Git is distributed, Ch 1). You go to the library to pick up photos others added, and to drop off yours.

    The Real Names#

    • A remote is another copy of the repository, usually on GitHub. The default name for the one you cloned from is origin.

    • git clone <url> — download a full copy of a repository, including all history.

    • git fetch — download new commits from the remote, without changing your branches. They appear as remote-tracking branches like origin/main — your local record of where the remote's branches were last time you looked.

    • git pull = git fetch + merge (or rebase, with --rebase) origin/main into your current branch.

    • git push — upload your commits to the remote and move its branch pointer.

    Under the Hood#

       GitHub (origin)                          Your laptop
       ───────────────                          ───────────
       main ──▶ C                    fetch      origin/main ──▶ C   (your record of GitHub's main)
                                   ────────▶    main ──▶ B          (your local main, behind)
                                                 pull = fetch + merge origin/main into main
                                    <────────
                                      push       your new commits go up; GitHub's main moves forward

    Connecting with SSH keys: GitHub authenticates you with the same kind of SSH key pair as servers (The Linux Command Line Ch 10): add your public key in GitHub's settings, then clone with git@github.com:org/studybuddy.git. (The alternative is HTTPS with a personal access token.)

    A normal day:

    Terminal
    git switch main
    git pull --rebase                   # get the latest
    git switch -c feature/quiz-timer    # new branch for today's work (Ch 4)
    # ... edit, then:
    git add -p && git commit -m "Add countdown timer to quizzes"
    git push -u origin feature/quiz-timer   # -u: remember that this branch tracks origin/feature/quiz-timer

    Push rejected? ! [rejected] main -> main (fetch first) means the remote has commits you don't have. Pull first, then push again. Don't reach for --force.

    git push --force replaces the remote branch with yours, deleting any commits on the remote that you don't have. On a shared branch that destroys teammates' work. If you really must (e.g. after rebasing your own feature branch), use git push --force-with-lease, which refuses if someone else pushed in the meantime.

    Check yourself

    4 questions
    1

    fetch vs pull?

    Show answer

    fetch downloads new commits without changing your branches; pull fetches and then merges (or rebases) them into your branch.

    2

    What is origin/main?

    Show answer

    Your local record of where the remote's main branch pointed the last time you fetched.

    3

    Your push is rejected with "fetch first". What happened, and what do you do?

    Show answer

    Someone else pushed commits you don't have. Pull (merge or rebase), resolve anything, then push again.

    4

    Why is --force-with-lease safer than --force?

    Show answer

    It refuses to overwrite the remote if it contains commits you haven't seen, so you can't silently delete others' work.