GitHub Basics
Connecting to remote repositories, pushing, pulling, and collaborating on GitHub.
What you'll learn
- What a remote repository is and how to add one
- Pushing your local commits with git push
- Pulling changes from others with git pull
- Cloning an existing repository with git clone
- The basics of pull requests for collaboration
Concept
What Is a Remote?
A remote is a version of your repository hosted on a server — like GitHub, GitLab, or Bitbucket. Your local repository can connect to one or more remotes to sync changes.
When you clone a repository, Git automatically sets up a remote named origin pointing to the source URL. If you created a repo locally, you add a remote manually:
git remote add origin https://github.com/username/repo.git
git push — Sending Changes Up
git push sends your local commits to the remote repository:
# First push: set up tracking and push to origin/main
git push -u origin main
# Subsequent pushes (tracking is already set up)
git push
The -u (or --set-upstream) flag tells Git to remember the connection between your local main branch and origin/main. After the first push, you can just type git push.
git pull — Getting Changes Down
git pull fetches changes from the remote and merges them into your local branch:
git pull
Always pull before you push if you are collaborating — someone else may have pushed changes that you do not have yet. If you push without pulling, Git will reject your push and ask you to pull first.
git clone — Copying a Repository
git clone downloads an entire repository, including all history, and sets up the remote:
git clone https://github.com/user/repo.git
cd repo
This is how you start working on an existing project — for example, contributing to open source.
SSH vs HTTPS
You can connect to GitHub using HTTPS or SSH:
- HTTPS — uses your username and a personal access token. Easier to set up, but you may be asked for credentials frequently.
- SSH — uses an SSH key pair. More secure and no repeated password entry, but requires generating and uploading a key.
# HTTPS URL
git clone https://github.com/user/repo.git
# SSH URL
git clone [email protected]:user/repo.git
Pull Requests (PRs)
A pull request is GitHub's collaboration feature. The workflow:
- Fork the repository (or create a branch if you have write access)
- Make your changes and commit them
- Push to your fork or branch
- Open a pull request on GitHub asking the maintainers to merge your changes
- Review — maintainers review your code, request changes, and eventually merge
Pull requests are where code review happens — they are the heart of team collaboration on GitHub.
Common Remote Commands
git remote -v # list all remotes
git remote add origin <url> # add a remote
git push -u origin main # push and set tracking
git pull # fetch and merge remote changes
git fetch # fetch without merging (safer)
git clone <url> # download a repositoryExample
# Scenario 1: Push a local repo to a new GitHub repository
# First, create the repo on GitHub (via the website), then:
git remote add origin https://github.com/username/my-project.git
git branch -M main # ensure the branch is called main
git push -u origin main
# Scenario 2: Clone an existing repository and make changes
git clone https://github.com/username/existing-project.git
cd existing-project
# Create a feature branch
git checkout -b add-login-feature
# Make changes, commit, and push the branch
echo "new feature" > login.js
git add login.js
git commit -m "feat: add login feature"
git push -u origin add-login-feature
# Now open a pull request on GitHub
# Scenario 3: Sync with the remote (collaboration)
git pull # get the latest changes from teammates
# resolve any conflicts, then:
git push
# List your remotes
git remote -v
# Fetch without merging (see what's new without changing your files)
git fetch
git log --oneline origin/main # see what the remote has
Three common workflows: pushing a local repo to GitHub, cloning and contributing via a branch + pull request, and syncing with teammates. The fetch + log pattern lets you preview remote changes before merging.
Try it
- SSH Key Generator
Generate an SSH key pair for secure GitHub authentication.
- CI/CD YAML Generator
Generate GitHub Actions pipelines that run on every push.
Common mistakes
The mistake
Forgetting to pull before pushing and getting a rejection error
The fix
When collaborating, always run git pull before git push. If someone else pushed changes, your push will be rejected until you pull and merge their work first.
The mistake
Using git pull without understanding it can create merge commits
The fix
git pull fetches AND merges. If you want to see what is new without changing your files, use git fetch first. Consider git pull --rebase to avoid extra merge commits in your history.
Related Resources
Related Tools
- .gitignore Generator
Generate a .gitignore before pushing to avoid committing secrets.
Related Snippets
- Git Remote Snippet
Advanced remote management commands.
Related Cheatsheets
- Git Cheatsheet
Remote, push, pull, and clone commands.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.