Git & GitHub Collaboration Guide

Welcome to the comprehensive guide on using Git and GitHub for collaborative development. This guide will walk you through setting up Git, creating repositories, understanding the basic workflow, and collaborating effectively with other developers.

Getting Started with Git and GitHub

Installing Git

First, you need to install Git on your local machine:

# For macOS (using Homebrew)
brew install git

# For Ubuntu/Debian
sudo apt update
sudo apt install git

# For Windows
# Download from https://git-scm.com/download/win

Configuring Git

Set up your identity in Git:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Creating a GitHub Account

If you don't have a GitHub account already:

  1. Go to github.com
  2. Click on "Sign up" and follow the instructions
  3. Verify your email address

Creating Your First Repository

To create a new repository on GitHub:

  1. Click the "+" icon in the top right corner
  2. Select "New repository"
  3. Fill in the repository name and description
  4. Choose public or private visibility
  5. Optionally add a README file, .gitignore, and license
  6. Click "Create repository"

Basic Git Workflow

Cloning a Repository

To get a copy of the repository on your local machine:

git clone https://github.com/username/repository.git

Creating and Switching Branches

Creating a new branch for your work:

# Create and switch to a new branch
git checkout -b feature-name

# List all branches
git branch

# Switch to an existing branch
git checkout branch-name

Making Changes

The basic workflow for making changes:

# Check status of your changes
git status

# Stage changes for commit
git add filename.ext # Stage specific file
git add . # Stage all changes

# Commit your changes
git commit -m "Descriptive commit message"

# Push changes to GitHub
git push origin branch-name

Pulling Changes from Remote

Keeping your local repository up to date:

# Update your current branch with remote changes
git pull origin branch-name

Collaborating with Other Developers

Fork and Pull Request Workflow

A common workflow for open-source projects:

  1. Fork the repository to your GitHub account
  2. Clone your fork to your local machine
  3. Create a new branch for your changes
  4. Make changes and commit them
  5. Push your branch to your fork
  6. Create a pull request from your fork to the original repository

Working on a Shared Repository

For team members with write access to the same repository:

  1. Clone the repository
  2. Create a branch for your feature or bugfix
  3. Make changes and commit them
  4. Push your branch to the remote repository
  5. Create a pull request for code review
  6. After approval, merge the pull request

Handling Merge Conflicts

When Git cannot automatically merge changes:

# Pull the latest changes from the target branch
git checkout main
git pull origin main

# Switch back to your branch and merge
git checkout your-branch
git merge main

# If conflicts occur, resolve them in your code editor
# After resolving, mark as resolved
git add resolved-file.ext

# Complete the merge
git commit -m "Merge main into your-branch"

Git and GitHub Best Practices

Commit Messages

  • Write clear, concise commit messages
  • Use present tense: "Add feature" not "Added feature"
  • First line should be under 50 characters
  • Include a detailed description if needed after the first line

Branching Strategy

  • Use feature branches for new features
  • Use bugfix branches for bug fixes
  • Keep the main branch stable and deployable
  • Consider using the Git Flow or GitHub Flow workflow

Pull Requests

  • Create descriptive pull request titles
  • Include a detailed description of changes
  • Link relevant issues
  • Request reviews from team members
  • Address feedback promptly

Code Reviews

  • Be respectful and constructive in comments
  • Focus on code, not the person
  • Look for clarity, correctness, and maintainability
  • Suggest improvements, not just point out issues

Additional Resources

Learning Resources

Useful Tools