Using Git and GitHub Effectively on a Team Software Project

Version control is a fundamental skill for any computer science student, especially when working on collaborative projects. Git and GitHub are industry-standard tools that help teams manage code, track changes, and collaborate efficiently. This guide provides a comprehensive overview of best practices and workflows for using Git and GitHub effectively on a team of 2-4 students working on a software project. The team will be using macOS, Windows laptops, and potentially a Linux server for development and deployment. VS Code will be the primary editor.


1. Understanding Git and GitHub

  • Git: A distributed version control system that tracks changes to your codebase, allowing multiple developers to work on a project simultaneously without overwriting each other’s work.
  • GitHub: A cloud-based platform for hosting Git repositories. It provides collaboration features like pull requests, issue tracking, and project boards.

2. Setting Up Your Environment

On macOS and Windows Laptops

  1. Install Git:
    • On macOS: Probably already installed. If not:
      brew install git
    • On Windows: Download and install Git from git-scm.com.
  2. Configure Git:
    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
  3. Set Up SSH for GitHub:
    • Generate an SSH key:
      ssh-keygen -t ed25519 -C "your.email@example.com"
    • Add the SSH key to your GitHub account (GitHub instructions).
  4. Install VS Code:
    • Download VS Code from code.visualstudio.com.
    • Install the GitHub extension and other relevant extensions (e.g., Prettier, ESLint).

On the Linux Server

  1. Install Git:
    sudo apt update && sudo apt install git
  2. Set Up SSH:
    • Repeat the SSH setup steps above for server access.
  3. Install VS Code Remote Development Tools:
    • Use the “Remote – SSH” extension in VS Code to edit files directly on the server.

3. Creating a Team Repository

  1. On GitHub:
    • One team member creates a new repository under their GitHub account or organization.
    • Add a meaningful name, description, and initialize with a README file.
    • Add team members as collaborators under “Settings > Manage Access.”
  2. Cloning the Repository:
    • Each member clones the repository to their local machine:git clone git@github.com:username/repository-name.git cd repository-name
      Each member clones the repository to their local machine:
      git clone git@github.com:username/repository-name.git cd repository-name
  3. Branching Strategy:
    • Agree on a branching strategy. A common model is Git Flow:
      • main branch: Production-ready code.
      • dev branch: Integration of completed features.
      • Feature branches: Short-lived branches for individual features.

4. Basic Git Workflow

  1. Creating a New Branch:
    git checkout -b feature/feature-name
  2. Making Changes:
    • Add and edit files in VS Code.
  3. Staging Changes:
    • Check the status:
      git status
    • Stage changes:
      git add file-name
      Or stage all changes:
      git add .
  4. Committing Changes:
    • Commit with a descriptive message:
      git commit -m "Added feature X"
  5. Pushing Changes:
    • Push your branch to GitHub:
      git push origin feature/feature-name
  6. Creating a Pull Request (PR):
    • On GitHub, navigate to your repository.
    • Open a PR from your feature branch into the dev or main branch.
    • Add a title and detailed description of your changes.
  7. Code Review:
    • Team members review the PR.
    • Discuss changes and request modifications if needed.
  8. Merging the PR:
    • After approval, merge the PR into the target branch.
    • Delete the feature branch on GitHub to keep the repository clean.

5. Resolving Merge Conflicts

  • When Conflicts Occur:
    • Fetch the latest changes:
      git pull origin target-branch
    • Resolve conflicts in VS Code by using the built-in conflict resolution tools.
    • Stage the resolved files:
      git add file-name
    • Commit the merge:
      git commit

6. Collaboration Best Practices

  1. Commit Messages:
    • Write meaningful commit messages that explain the “what” and “why.”
    • Use present tense (e.g., “Fix bug in login module”).
  2. Pull Regularly:
    • Keep your local branch up-to-date by pulling changes frequently:
      git pull origin branch-name
  3. Review and Test Code:
    • Always review PRs carefully.
    • Test changes locally or on the Linux server before merging.
  4. Use Issues and Project Boards:
    • Use GitHub Issues to track tasks, bugs, and features.
    • Organize work using GitHub Projects for visual tracking.
  5. Backup Work:
    • Push your work frequently to avoid losing progress.

7. Advanced Tips for Team Efficiency

  1. Git Aliases:
    • Speed up common commands with aliases:
      git config --global alias.co checkout
      git config --global alias.br branch
      git config --global alias.st status
      git config --global alias.cm "commit -m"
  2. Pre-Commit Hooks:
    • Automate checks (e.g., linting, tests) before committing changes using Git hooks.
  3. Squashing Commits:
    • Combine multiple commits into one for cleaner history:
      git rebase -i HEAD~n
  4. GitHub Actions:
    • Automate workflows like testing and deployment with GitHub Actions.
  5. Remote Development:
    • Use VS Code’s “Remote – SSH” extension to develop directly on the Linux server.

8. Troubleshooting Common Issues

  1. Detached HEAD:
    • If you see “detached HEAD,” switch to a branch:
      git checkout branch-name
  2. Reverting Changes:
    • Undo the last commit:
      git revert HEAD
  3. Recovering Lost Work:
    • Find lost commits:
      git reflog
    • Restore a specific commit:
      git checkout commit-hash

Conclusion

By following these workflows and best practices, you’ll collaborate more effectively with your team while maintaining a clean, well-documented codebase. Git and GitHub are powerful tools—the more you use them, the more confident you’ll become in managing collaborative projects. Happy coding!

Scroll to Top