Activity

Hands-on Git Exercises

Practice these exercises to reinforce your understanding of Git workflows and collaboration.

Exercise 1: Setting Up a Repository with Multiple Remotes

  1. Create a new repository on both GitHub and GitLab
  2. Initialize a local repository and add both remotes
  3. Create a README.md file and push it to both remotes

Start with this template:

# Initialize local repository
mkdir git-practice
cd git-practice
git init

# Create README file
echo "# Git Practice Repository" > README.md
git add README.md
git commit -m "Initial commit"

# Add remotes (replace with your own URLs)
git remote add github https://github.com/username/git-practice.git
git remote add gitlab https://gitlab.com/username/git-practice.git

# Push to both remotes
git push github main
git push gitlab main

Exercise 2: Feature Branch Development

  1. Create a feature branch from main
  2. Make several commits to the feature branch
  3. Keep your feature branch updated with changes from main
  4. Create a merge request

Follow these steps:

# Make sure you're on main and it's up to date
git checkout main
git pull origin main

# Create and switch to a feature branch
git checkout -b feature/add-documentation

# Make changes and commit
echo "## Installation" >> README.md
echo "Run \`npm install\` to install dependencies." >> README.md
git add README.md
git commit -m "Add installation instructions"

# Make more changes
echo "## Usage" >> README.md
echo "Run \`npm start\` to start the application." >> README.md
git add README.md
git commit -m "Add usage instructions"

# Update feature branch with changes from main (if any)
git checkout main
git pull origin main
git checkout feature/add-documentation
git rebase main

# Push feature branch to remote
git push origin feature/add-documentation

# Now create a merge request through the GitHub/GitLab web interface

Exercise 3: Resolving Merge Conflicts

  1. Create two branches that modify the same file
  2. Merge one branch into main
  3. Try to merge the second branch and resolve the conflicts

Try this scenario:

# Start from main
git checkout main

# Create first feature branch
git checkout -b feature/add-contributing
echo "## Contributing" >> README.md
echo "Please follow the code style guidelines." >> README.md
git add README.md
git commit -m "Add contributing guidelines"
git checkout main
git merge feature/add-contributing
git push origin main

# Create second feature branch (from earlier main state)
git checkout -b feature/add-license
echo "## License" >> README.md
echo "This project is licensed under MIT." >> README.md
git add README.md
git commit -m "Add license information"

# Try to merge - this will cause a conflict
git checkout main
git merge feature/add-license

# Resolve the conflict manually, then
git add README.md
git commit -m "Merge feature/add-license, resolve conflicts"
git push origin main

Exercise 4: Interactive Rebasing

  1. Create a feature branch with several small commits
  2. Use interactive rebasing to clean up the commit history
  3. Push the cleaned-up branch

Follow these steps:

# Create a feature branch
git checkout -b feature/api-integration

# Make several small commits
echo "function fetchData() {" > api.js
echo "  // TODO: Implement" >> api.js
echo "}" >> api.js
git add api.js
git commit -m "Add fetchData function skeleton"

echo "function fetchData() {" > api.js
echo "  return fetch('https://api.example.com/data')" >> api.js
echo "    .then(response => response.json());" >> api.js
echo "}" >> api.js
git add api.js
git commit -m "Implement fetchData function"

echo "function fetchData() {" > api.js
echo "  return fetch('https://api.example.com/data')" >> api.js
echo "    .then(response => response.json())" >> api.js
echo "    .catch(error => console.error('Error fetching data:', error));" >> api.js
echo "}" >> api.js
git add api.js
git commit -m "Add error handling to fetchData"

# Use interactive rebase to clean up commits
git rebase -i HEAD~3

# In the editor, change the second and third commits from "pick" to "squash"
# Save and close, then edit the commit message
# After rebasing, force push (only for private branches!)
git push origin feature/api-integration --force

Exercise 5: Creating a Complete Pull Request

  1. Identify a small improvement for an open-source project
  2. Fork the repository
  3. Create a feature branch with your improvement
  4. Submit a pull request with proper documentation

This exercise should be done on a real GitHub/GitLab repository:

# Fork the repository through the GitHub/GitLab interface

# Clone your fork
git clone https://github.com/your-username/project.git
cd project

# Create a feature branch
git checkout -b fix/update-readme

# Make your changes
# Edit files as needed

# Commit your changes
git add .
git commit -m "Update README with clearer installation instructions"

# Push to your fork
git push origin fix/update-readme

# Create a pull request through the GitHub/GitLab interface
# Include a detailed description of your changes
Previous