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:
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.email "your.email@example.com"
Creating a GitHub Account
If you don't have a GitHub account already:
- Go to github.com
- Click on "Sign up" and follow the instructions
- Verify your email address
Creating Your First Repository
To create a new repository on GitHub:
- Click the "+" icon in the top right corner
- Select "New repository"
- Fill in the repository name and description
- Choose public or private visibility
- Optionally add a README file, .gitignore, and license
- Click "Create repository"
Basic Git Workflow
Cloning a Repository
To get a copy of the repository on your local machine:
Creating and Switching Branches
Creating a new branch for your work:
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:
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:
git pull origin branch-name
Collaborating with Other Developers
Fork and Pull Request Workflow
A common workflow for open-source projects:
- Fork the repository to your GitHub account
- Clone your fork to your local machine
- Create a new branch for your changes
- Make changes and commit them
- Push your branch to your fork
- 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:
- Clone the repository
- Create a branch for your feature or bugfix
- Make changes and commit them
- Push your branch to the remote repository
- Create a pull request for code review
- After approval, merge the pull request
Handling Merge Conflicts
When Git cannot automatically merge changes:
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
- Pro Git Book - Comprehensive guide to Git
- GitHub Documentation - Official GitHub guides
- Learn Git Branching - Interactive tutorial
- Atlassian Git Tutorials - In-depth Git tutorials
Useful Tools
- GitHub Desktop - GUI for Git and GitHub
- GitKraken - Visual Git client
- Sourcetree - Free Git GUI for Windows and Mac
- Gitmoji - Emoji guide for commit messages