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
- Install Git:
- On macOS: Probably already installed. If not:
brew install git - On Windows: Download and install Git from git-scm.com.
- On macOS: Probably already installed. If not:
- Configure Git:
git config --global user.name "Your Name"git config --global user.email "your.email@example.com" - 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).
- Generate an SSH key:
- 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
- Install Git:
sudo apt update && sudo apt install git - Set Up SSH:
- Repeat the SSH setup steps above for server access.
- 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
- 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.”
- 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
- Each member clones the repository to their local machine:
- Branching Strategy:
- Agree on a branching strategy. A common model is Git Flow:
mainbranch: Production-ready code.devbranch: Integration of completed features.- Feature branches: Short-lived branches for individual features.
- Agree on a branching strategy. A common model is Git Flow:
4. Basic Git Workflow
- Creating a New Branch:
git checkout -b feature/feature-name - Making Changes:
- Add and edit files in VS Code.
- Staging Changes:
- Check the status:
git status - Stage changes:
git add file-name
Or stage all changes:git add .
- Check the status:
- Committing Changes:
- Commit with a descriptive message:
git commit -m "Added feature X"
- Commit with a descriptive message:
- Pushing Changes:
- Push your branch to GitHub:
git push origin feature/feature-name
- Push your branch to GitHub:
- Creating a Pull Request (PR):
- On GitHub, navigate to your repository.
- Open a PR from your feature branch into the
devormainbranch. - Add a title and detailed description of your changes.
- Code Review:
- Team members review the PR.
- Discuss changes and request modifications if needed.
- 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
- Fetch the latest changes:
6. Collaboration Best Practices
- Commit Messages:
- Write meaningful commit messages that explain the “what” and “why.”
- Use present tense (e.g., “Fix bug in login module”).
- Pull Regularly:
- Keep your local branch up-to-date by pulling changes frequently:
git pull origin branch-name
- Keep your local branch up-to-date by pulling changes frequently:
- Review and Test Code:
- Always review PRs carefully.
- Test changes locally or on the Linux server before merging.
- Use Issues and Project Boards:
- Use GitHub Issues to track tasks, bugs, and features.
- Organize work using GitHub Projects for visual tracking.
- Backup Work:
- Push your work frequently to avoid losing progress.
7. Advanced Tips for Team Efficiency
- Git Aliases:
- Speed up common commands with aliases:
git config --global alias.co checkoutgit config --global alias.br branchgit config --global alias.st statusgit config --global alias.cm "commit -m"
- Speed up common commands with aliases:
- Pre-Commit Hooks:
- Automate checks (e.g., linting, tests) before committing changes using Git hooks.
- Squashing Commits:
- Combine multiple commits into one for cleaner history:
git rebase -i HEAD~n
- Combine multiple commits into one for cleaner history:
- GitHub Actions:
- Automate workflows like testing and deployment with GitHub Actions.
- Remote Development:
- Use VS Code’s “Remote – SSH” extension to develop directly on the Linux server.
8. Troubleshooting Common Issues
- Detached HEAD:
- If you see “detached HEAD,” switch to a branch:
git checkout branch-name
- If you see “detached HEAD,” switch to a branch:
- Reverting Changes:
- Undo the last commit:
git revert HEAD
- Undo the last commit:
- Recovering Lost Work:
- Find lost commits:
git reflog - Restore a specific commit:
git checkout commit-hash
- Find lost commits:
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!
