- Practice essential remote repository operations
- Learn to handle remote branch management
- Master collaboration workflows with remotes
- Implement proper remote synchronization practices
Practice Overview
These exercises will help you master working with remote repositories through practical scenarios. You'll practice common collaboration workflows and learn to handle various remote operations effectively.
Exercise 1: Remote Repository Setup
Scenario
You have a local project that needs to be shared with your team. Set up a remote repository and push your existing project to it.
Steps:
- Create a new remote repository
- Add the remote to your local repository
- Push your existing code
- Verify the remote setup
Solution:
# Add remote repository$ git remote add origin https://github.com/username/repository.git # Verify remote$ git remote -v # Push existing code$ git push -u origin main # List remote branches$ git branch -r # Show remote details$ git remote show origin
Exercise 2: Basic Collaboration Workflow
Scenario
Practice a typical collaboration workflow where you need to work with a teammate's changes while pushing your own updates.
Workflow Steps:
# Get latest changes$ git fetch origin$ git pull origin main # Create feature branch$ git checkout -b feature/new-feature # Make changes$ touch new-feature.txt$ git add new-feature.txt$ git commit -m "Add new feature" # Push feature branch$ git push -u origin feature/new-feature # Update with main branch changes$ git checkout main$ git pull origin main$ git checkout feature/new-feature$ git merge main # Resolve any conflicts and push$ git push origin feature/new-feature
Key Points:
- Always fetch and pull before starting new work
- Work in feature branches to isolate changes
- Regularly sync with the main branch
Exercise 3: Remote Branch Management
Scenario
Practice managing remote branches, including tracking, deleting, and handling upstream changes.
Tasks:
- Set up tracking branches
- Delete remote branches
- Handle upstream branch deletion
- Update remote branch list
Commands:
# List remote branches$ git branch -r # Set up tracking branch$ git checkout --track origin/feature/user-profile # Delete remote branch$ git push origin --delete feature/old-feature # Prune deleted remote branches$ git fetch --prune # Push to different remote branch$ git push origin local-branch:remote-branch # Rename remote branch$ git push origin :old-name new-name
Exercise 4: Advanced Remote Operations
Scenario
Work with multiple remotes and handle more complex remote operations.
Implementation:
# Add multiple remotes$ git remote add upstream https://github.com/original/repository.git$ git remote add staging https://github.com/company/staging.git # Fetch from specific remote$ git fetch upstream$ git fetch staging # Push to multiple remotes$ git push origin main$ git push staging main # Sync with upstream$ git fetch upstream$ git merge upstream/main # Show remote branch relationships$ git remote show origin$ git remote show upstream
Remote Operation Best Practices
- Always fetch before pulling to check for changes
- Use
--prune
regularly to keep your remote branch list clean - Configure push.default to avoid accidental pushes
- Use SSH keys for secure authentication with remotes