Dev Logs
/Git & GitHub/ Remote Repositories and GitHub
Chapters
  • 01Git and GitHub Introduction
  • 02Basic Git Commands
  • 03Remote Repositories and GitHub
    • Understanding Remote Repositories
    • Creating a Repository on GitHub
    • Step 1: Create Repository on GitHub
    • Step 2: Connect Local Repository to GitHub
    • git remote - Managing Remote Repositories
    • git push - Upload Changes to Remote
    • First Push Example
    • git pull - Download Changes from Remote
    • git fetch vs git pull
    • Cloning Repositories
    • git clone - Copy Remote Repository
    • Practical Example: Contributing to Open Source
    • .gitignore - Ignoring Files
    • Creating .gitignore
    • Common .gitignore Patterns
    • .gitignore for Our Portfolio Project
    • .gitignore Rules
    • Working with Existing Repositories
    • Scenario 1: Join an Existing Project
    • Scenario 2: Sync Your Fork
    • Authentication with GitHub
    • Using Personal Access Tokens (Recommended)
    • Using SSH Keys (Advanced)
    • Practical Workflow Example
    • Troubleshooting Common Issues
    • Issue 1: Push Rejected
    • Issue 2: Merge Conflicts
    • Issue 3: Wrong Remote URL
    • Best Practices
    • Quick Reference
  • 04Branching Basics
  • 05Git Merge vs Rebase
  • 06Git Stash
  • 07Git Diff and Log
  • 08Resolving Merge Conflicts
  • 09Pull Requests and Code Review
  • 10Question about implementation
  • 11Positive feedback
  • 12Security concern
  • 13Forking and Upstream Remotes
  • 14Advanced Git Rebase
  • 15Git Cherry-pick
  • 16Git Reset Deep Dive
  • 17Git Reflog Recovery
  • 18Git Bisect Bug Hunting
  • 19Git Tags
  • 20Git Hooks
  • 21Add hooks to version control
  • 22Conventional Commits
  • 23Update README
  • 24Conventional Commits Demo
  • 25GitHub CLI (gh)
  • 26GitHub Actions Basics
  • 27Maintaining Clean Git History in Teams
  • 28Branching Strategies: Git Flow vs Trunk-Based Development
  • 29Force Push Safety: Using --force-with-lease
All chapters

Remote Repositories and GitHub

This chapter covers working with remote repositories, connecting to GitHub, and synchronizing your local work with the cloud.

Understanding Remote Repositories

A remote repository is a version of your project hosted on a server (like GitHub, GitLab, or Bitbucket). It allows:

  • Backup: Your code is safely stored in the cloud
  • Collaboration: Multiple developers can work on the same project
  • Sharing: Others can access and contribute to your code
  • Deployment: Automated deployments from your repository

Creating a Repository on GitHub

Step 1: Create Repository on GitHub

  1. Go to GitHub.com and sign in
  2. Click the "+" icon → "New repository"
  3. Fill in repository details:
    • Repository name: portfolio-website
    • Description: "My personal portfolio website"
    • Visibility: Public or Private
    • Initialize: Don't check any boxes (we already have local files)
  4. Click "Create repository"

Step 2: Connect Local Repository to GitHub

bash
# Add remote origin (replace with your GitHub username)
git remote add origin https://github.com/yourusername/portfolio-website.git

# Verify remote was added
git remote -v

Output:

origin  https://github.com/yourusername/portfolio-website.git (fetch)
origin  https://github.com/yourusername/portfolio-website.git (push)

git remote - Managing Remote Repositories

bash
# List all remotes
git remote
git remote -v  # Show URLs

# Add a remote
git remote add <name> <url>
git remote add origin https://github.com/user/repo.git

# Remove a remote
git remote remove <name>
git remote rm origin

# Rename a remote
git remote rename <old-name> <new-name>
git remote rename origin upstream

# Change remote URL
git remote set-url origin https://github.com/user/new-repo.git

# Show remote details
git remote show origin

git push - Upload Changes to Remote

The git push command uploads your local commits to a remote repository.

bash
# Push to remote repository
git push <remote> <branch>
git push origin main

# Push and set upstream (first time)
git push -u origin main
# or
git push --set-upstream origin main

# Push all branches
git push --all origin

# Push tags
git push --tags

# Force push (dangerous!)
git push --force origin main
# Safer force push
git push --force-with-lease origin main

First Push Example

bash
# In our portfolio project
cd portfolio-website

# Push to GitHub for the first time
git push -u origin main

Output:

Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 8 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (9/9), 1.2 KiB | 1.2 MiB/s, done.
Total 9 (delta 1), reused 0 (delta 0)
To https://github.com/yourusername/portfolio-website.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

git pull - Download Changes from Remote

The git pull command downloads and merges changes from a remote repository.

bash
# Pull from remote
git pull <remote> <branch>
git pull origin main

# Pull with rebase instead of merge
git pull --rebase origin main

# Pull all branches
git pull --all

git fetch vs git pull

bash
# git fetch: Download changes but don't merge
git fetch origin
git fetch origin main

# git pull: Download and merge changes
git pull origin main
# Equivalent to:
git fetch origin main
git merge origin/main

Cloning Repositories

git clone - Copy Remote Repository

bash
# Clone a repository
git clone <url>
git clone https://github.com/user/repo.git

# Clone to specific directory
git clone https://github.com/user/repo.git my-project

# Clone specific branch
git clone -b develop https://github.com/user/repo.git

# Shallow clone (recent history only)
git clone --depth 1 https://github.com/user/repo.git

Practical Example: Contributing to Open Source

bash
# Clone a popular repository
git clone https://github.com/microsoft/vscode.git
cd vscode

# Check remote configuration
git remote -v

# Check current branch
git branch

# View recent commits
git log --oneline -5

.gitignore - Ignoring Files

The .gitignore file tells Git which files to ignore.

Creating .gitignore

bash
# Create .gitignore file
touch .gitignore
# or on Windows
echo. > .gitignore

Common .gitignore Patterns

gitignore
# Dependencies
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Environment variables
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# Build outputs
build/
dist/
*.min.js
*.min.css

# IDE files
.vscode/
.idea/
*.swp
*.swo
*~

# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# Logs
logs
*.log

# Temporary files
*.tmp
*.temp

# Compiled files
*.class
*.o
*.pyc
__pycache__/

# Archives
*.zip
*.tar.gz
*.rar

# Database
*.sqlite
*.db

.gitignore for Our Portfolio Project

bash
# Create .gitignore for portfolio
cat > .gitignore << EOF
# Development files
*.log
*.tmp

# OS files
.DS_Store
Thumbs.db

# Editor files
.vscode/
.idea/

# Build files
dist/
build/
EOF

# Add and commit .gitignore
git add .gitignore
git commit -m "Add .gitignore file"
git push origin main

.gitignore Rules

gitignore
# Ignore specific file
config.txt

# Ignore all files with extension
*.log

# Ignore directory
node_modules/

# Ignore files in directory
logs/*.log

# Ignore files in all subdirectories
**/*.log

# Don't ignore specific file (exception)
!important.log

# Ignore files only in root
/config.txt

# Comments start with #
# This is a comment

Working with Existing Repositories

Scenario 1: Join an Existing Project

bash
# Clone the project
git clone https://github.com/company/project.git
cd project

# Check project structure
ls -la

# View recent activity
git log --oneline -10

# Check branches
git branch -a

# Install dependencies (if applicable)
npm install  # for Node.js projects
# or
pip install -r requirements.txt  # for Python projects

Scenario 2: Sync Your Fork

bash
# Add upstream remote (original repository)
git remote add upstream https://github.com/original/repo.git

# Fetch upstream changes
git fetch upstream

# Switch to main branch
git checkout main

# Merge upstream changes
git merge upstream/main

# Push updated main to your fork
git push origin main

Authentication with GitHub

Using Personal Access Tokens (Recommended)

  1. Go to GitHub Settings → Developer settings → Personal access tokens
  2. Generate new token with appropriate scopes
  3. Use token as password when prompted
bash
# When pushing, use token as password
git push origin main
# Username: yourusername
# Password: ghp_xxxxxxxxxxxxxxxxxxxx (your token)

Using SSH Keys (Advanced)

bash
# Generate SSH key
ssh-keygen -t ed25519 -C "your.email@example.com"

# Add to SSH agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

# Copy public key to clipboard
cat ~/.ssh/id_ed25519.pub
# Add this to GitHub Settings → SSH and GPG keys

# Test connection
ssh -T git@github.com

# Use SSH URL for remote
git remote set-url origin git@github.com:username/repo.git

Practical Workflow Example

Let's simulate a complete workflow:

bash
# 1. Make changes locally
echo "<p>Updated portfolio with new projects</p>" >> index.html

# 2. Stage and commit
git add index.html
git commit -m "Update portfolio with new projects section"

# 3. Push to GitHub
git push origin main

# 4. Simulate collaborator changes (on GitHub web interface)
# Edit README.md directly on GitHub and commit

# 5. Pull collaborator changes
git pull origin main

# 6. View updated history
git log --oneline -5

Troubleshooting Common Issues

Issue 1: Push Rejected

bash
# Error: Updates were rejected because the remote contains work
git pull origin main  # Pull first
git push origin main  # Then push

Issue 2: Merge Conflicts

bash
# When pulling creates conflicts
git pull origin main
# Fix conflicts in files
git add .
git commit -m "Resolve merge conflicts"
git push origin main

Issue 3: Wrong Remote URL

bash
# Check current remote
git remote -v

# Update remote URL
git remote set-url origin https://github.com/correct/repo.git

Best Practices

  1. Always Pull Before Push: Avoid conflicts by staying up-to-date
  2. Use Meaningful Commit Messages: Help collaborators understand changes
  3. Keep .gitignore Updated: Don't commit sensitive or generated files
  4. Use Branches: Don't work directly on main for features
  5. Regular Backups: Push frequently to avoid losing work

Quick Reference

bash
# Remote management
git remote add origin <url>    # Add remote
git remote -v                  # List remotes
git remote show origin         # Show remote details

# Synchronization
git push origin main           # Push to remote
git pull origin main           # Pull from remote
git fetch origin              # Download without merging

# Repository operations
git clone <url>               # Clone repository
git clone <url> <directory>   # Clone to specific directory

# .gitignore
echo "file.txt" >> .gitignore # Add file to ignore
git add .gitignore            # Commit .gitignore changes

Previous: Basic Git Commands
Next: Branching Basics

PreviousBasic Git CommandsNextBranching Basics

Open source, free forever. Built by iammhador.

Contribute on GitHub