Remotes & Push
Connect your repo to GitHub and share your work
Skills taught: git.remote, git.push
Connecting to a remote
# See existing remotes
git remote -v
# origin https://github.com/prashantkeshr/KERNAL.git (fetch)
# origin https://github.com/prashantkeshr/KERNAL.git (push)
# Add a remote (for a fresh git init repo)
git remote add origin https://github.com/yourname/my-project.git
# Change a remote URL (e.g., after repo rename)
git remote set-url origin https://github.com/yourname/new-name.git
# Remove a remote
git remote remove origin
# Add a second remote (e.g., for deploying to a different host)
git remote add deploy https://git.heroku.com/my-app.git
git push
# First push — set upstream tracking with -u
git push -u origin main
# From now on, just:
git push
# Push a specific branch
git push origin feature/login
# Push all branches
git push origin --all
# Push tags
git push origin --tags
# Delete a remote branch
git push origin --delete feature/old-branch
# Safe force push (fails if remote has new commits you haven't seen)
git push --force-with-lease
The full new-project workflow
# 1. Create repo on GitHub (via github.com UI)
# 2. Init local project
mkdir my-project && cd my-project
git init
git switch -c main # or: git config init.defaultBranch main
# 3. Add files and commit
echo '# My Project' > README.md
git add README.md
git commit -m "Initial commit"
# 4. Connect to GitHub
git remote add origin https://github.com/yourname/my-project.git
# 5. Push
git push -u origin main
Frequently Asked Questions
Why is my push rejected with 'Updates were rejected'?
The remote has commits you don't have locally. Someone else pushed first, or you pushed from another machine. Run git pull (or git fetch + git merge) to bring those commits in, then push again.
HTTPS vs SSH — which should I use?
HTTPS is easier to set up. Use a personal access token (PAT) as the password. SSH requires generating an ed25519 key pair and adding the public key to GitHub settings — but once done, it's seamless: no password prompts, no token rotation.