🔗 Connect Your Local Repo to Remote
Local repo is useless without remote. git remote connects to GitHub, GitLab, Bitbucket. Push, pull, collaborate.
📝 Adding Remote
# Add remote (HTTPS) git remote add origin https://github.com/username/repo.git # Add remote (SSH) git remote add origin git@github.com:username/repo.git # Set upstream branch git push -u origin main # List remotes git remote -v # Show remote details git remote show origin # Change remote URL git remote set-url origin https://new-url.com/repo.git # Remove remote git remote remove origin
🎯 Multiple Remotes
# Add multiple remotes git remote add origin https://github.com/user/repo.git git remote add backup https://gitlab.com/user/repo.git # Push to both git push origin main git push backup main # Push to all remotes at once git remote | xargs -L1 git push --all # Fetch from all remotes git remote update
💡 Authentication
- HTTPS: needs username/password or personal access token
- SSH: needs SSH key configured (more secure)
- GitHub: personal access token (PAT) recommended
- GitLab: personal access token
- Use SSH for frequent pushes (no password)
“Forgot to add remote, couldn’t push. Added git remote add origin. Connected to GitHub. Now push works. Remote is essential for collaboration.”
