Day 4: Remote Repositories

Remote Repositories

Understanding Remote Repositories

Remote repositories enable collaboration and serve as backups. Popular hosting services include GitHub, GitLab, Bitbucket, and Azure DevOps.

Setting Up Your First Remote

GitHub Setup:

bash

# Link local repository to GitHub
git remote add origin https://github.com/yourusername/my-awesome-project.git

# Verify remote configuration
git remote -v

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

Remote Workflow Commands

Pushing Changes:

bash

# Push current branch to remote
git push

# Push specific branch
git push origin feature/new-feature

# Push all branches
git push --all origin

Fetching and Pulling:

bash

# Fetch changes without merging
git fetch origin

# Pull changes (fetch + merge)
git pull origin main

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

Collaboration Workflow

Cloning Existing Repository:

bash

# Clone a repository
git clone https://github.com/username/repository-name.git
cd repository-name

# Check remote configuration
git remote -v

Working with Pull Requests:

bash

# Create feature branch
git switch -c feature/improve-performance

# Make changes and commit
git add .
git commit -m "Optimize database queries for better performance"

# Push branch to remote
git push -u origin feature/improve-performance

# Create Pull Request through GitHub/GitLab web interface

Managing Multiple Remotes

bash

# Add additional remote (e.g., upstream for forks)
git remote add upstream https://github.com/original-owner/repository.git

# Fetch from upstream
git fetch upstream

# Sync fork with upstream
git switch main
git merge upstream/main
git push origin main

Hands-On Exercise: Create a GitHub repository, push your project, invite a collaborator, and practice the pull request workflow.

You Might Also Like