CH.13 · PART 3

Your work.
Shared safely.

Clone, fetch, pull and push are different steps. Learn the direction before running the command.

LOCALSHAREDCheck where the work goes.

Local and remote are different copies

Your commits start on your computer. A remote is a named repository address, often hosted on GitHub. origin is the usual name assigned by clone, not a special server and not proof that an address is safe.

Before a push, inspect git remote -v. Check the owner, repository and branch. Never put a token in a remote URL or paste credentials into screenshots.

Choose the direction

REMOTEGitHub: originYOUR COMPUTERorigin/mainyour copy of the remotemainyour branchcreates both

clone: the remote history is copied into a new local repository, and origin/main and main are created.

Start from an existing repository

git clone REPOSITORY_URL
cd REPOSITORY_DIRECTORY
git remote -v
git status

REPOSITORY_URL and REPOSITORY_DIRECTORY are placeholders. Copy the real clone URL from the repository's Code menu. Use HTTPS or SSH according to how you authenticate. Cloning private repositories requires access.

Download is not integrate

fetch

Downloads objects and updates remote-tracking references. It does not merge them into your working branch.

pull

Fetches, then integrates with your current branch. The integration can merge or rebase depending on options and configuration.

push

Sends commits and updates a branch in the remote repository. It requires permission and can be rejected.

A careful daily loop

git status
git fetch origin
git log --oneline --graph --all --decorate
git pull --ff-only
# Make and test your change, then stage and commit.
git push -u origin YOUR_BRANCH

This loop assumes your current branch has an upstream and the downloaded history can fast-forward it. --ff-only refuses a diverged history instead of choosing a merge or rebase for you. If it refuses, inspect the graph and follow your team's workflow. Do not solve rejection by force-pushing.

-u records an upstream relationship after a successful push. YOUR_BRANCH is a placeholder for the branch you intend to publish.

Practice: the daily loop

hands-on

The remote here is simulated, so nothing leaves your browser. Clone a repo, watch a teammate push, fetch, pull, change a file and push.

1

Loading

practice terminal · bash-style

About this practice

The remote is a model. Real GitHub also asks you to sign in, and Chapter 15 covers that. The command output here follows real Git wording, but real servers can print extra lines.

Quick check

1 / 3

Pick an answer.

Sources

git clone · git fetch · git pull · git push

Stuck? Go deeper

optional