Remotes, fetch, pull and push
Share work through a remote repository and understand what each command moves.
A remote is another copy of the repository, usually on GitHub, GitLab or a server. By convention the main one is called origin.
git remote -v # list remotes
git clone https://example.com/team/app.git
git fetch origin # download new commits, change nothing locally
git pull # fetch + merge into the current branch
git push -u origin my-branch # upload and remember the upstreamFetch vs pull
git fetch only updates your view of the remote (origin/main). git pull fetches and then integrates the changes into your branch. When you want to look before you merge, fetch first.
A pull request workflow
- Update main:
git switch main && git pull. - Create a branch:
git switch -c fix-login-bug. - Commit small, focused changes.
- Push the branch and open a pull request (PR) so others can review it.
- Fix review comments with new commits, then merge when approved.
CarefulPush is rejected when the remote has commits you do not have. Pull (or rebase) first. Never solve it with git push --force on a shared branch: it erases other people's work.
Notegit push --force-with-lease is the safer force: it refuses if someone else pushed since your last fetch. Use it only on branches that are yours.
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.