A remote is a copy of your repository hosted somewhere else — usually GitHub, GitLab, or Bitbucket.
git remote -vorigin https://github.com/boringcollege/git-by-example.git (fetch)
origin https://github.com/boringcollege/git-by-example.git (push)
🧠 What happened? origin is the default name for the remote you cloned from. You can have multiple remotes.
git remote add upstream https://github.com/original-author/project.gitgit remote remove upstreamgit remote rename origin githubFetch downloads new data from the remote but does not change your working files.
git fetch originremote: Enumerating objects: 5, done.
remote: Total 3 (delta 1), reused 3 (delta 1)
Unpacking objects: 100% (3/3), done.
From https://github.com/boringcollege/git-by-example
a1b2c3d..d4e5f6g main -> origin/main
git log main..origin/main --onelined4e5f6g Fix typo in README
c3d4e5f Add contributing guidelines
🧠 What happened? git fetch updated your remote-tracking branches (origin/main) without touching your local main. You can inspect the changes before deciding to integrate them.
Pull = fetch + merge (or fetch + rebase).
git pull origin maingit pull --rebase origin maingit config --global pull.rebase truegit push origin maingit switch -c feature/dark-mode
# ... make commits ...
git push -u origin feature/dark-modeBranch 'feature/dark-mode' set up to track remote branch 'feature/dark-mode' from 'origin'.
🧠 What happened? -u (or --set-upstream) links your local branch to the remote branch. After this, you can just run git push without specifying the remote and branch.
git push origin main! [rejected] main -> main (fetch first)
error: failed to push some refs
hint: Updates were rejected because the remote contains work that you do not have locally.
Fix: pull first, then push.
git pull --rebase origin main
git push origin maingit push --force-with-lease origin feature/my-branch🧠 What happened? --force-with-lease is a safer force push. It refuses to overwrite the remote if someone else has pushed commits you haven't seen. Use it instead of --force.
git branch -vv feature/login f7g8h9i [origin/feature/login] Add login page
* main e4f5a6b [origin/main] Add greet function
git branch --set-upstream-to=origin/main main