33 KiB
# Git and GitHub Standards
Table of Contents
- Overview
- Understanding Git and GitHub
- Branching Strategy
- Branch Naming Conventions
- Commit Message Standards
- Code Review Policy
- Best Practices for Using Git
- Working with Pull Requests
- File-Path Standards
Overview
This document provides a comprehensive set of standards and best practices for using Git and GitHub within our development team. It covers foundational topics, including the branching strategy, branch naming conventions, commit message guidelines, code review policies, and collaboration workflows. The goal is to maintain a consistent and organized approach to version control, improve collaboration, ensure code quality, and streamline the development process.
The guidelines in this document are designed to help both new and experienced team members align with the company's coding and collaboration standards, facilitating smoother project management and code integrity.
Git and GitHub: A Comprehensive Guide
Table of Contents
- Introduction
- Understanding Version Control
- Git Basics
- Getting Started with Git
- Git Essentials
- Introduction to GitHub
- Working with Repositories on GitHub
- Git and GitHub Workflow
- Git Commands in Depth
- Advanced Git Techniques
- Common Mistakes and How to Avoid Them
Introduction
Git and GitHub are essential tools in the modern development landscape, allowing developers to manage changes, work collaboratively, and maintain robust codebases. Git is a distributed version control system that tracks code history, while GitHub enhances this by offering a platform for hosting, reviewing, and managing projects.
This guide delves into both Git and GitHub, providing an extensive overview that covers everything from beginner concepts to advanced techniques and command usage.
Understanding Version Control
Version Control is the backbone of any collaborative software development project. It allows teams to track code changes, collaborate seamlessly, and maintain a history of modifications to revert to previous versions when needed.
Types of Version Control
- Local Version Control: Simple, single-machine control, not suitable for team environments.
- Centralized Version Control (CVCS): Stores all file versions on a central server (e.g., SVN).
- Distributed Version Control (DVCS): Each team member has a full copy of the history and the latest version of the project, with Git being the most prominent DVCS.
Git is distributed, so it allows every developer to maintain a complete copy of the codebase on their own system, making collaboration more resilient and flexible.
Git Basics
What is Git?
Git is a distributed version control system (DVCS) designed to handle projects of all sizes efficiently. It tracks changes to files, enabling developers to manage, review, and revert code.
Core Concepts of Git
- Repository: A collection of files, folders, and their complete revision history.
- Commit: A snapshot of changes with a unique identifier (hash).
- Branch: A separate line of development within a repository.
- Merge: Combines changes from one branch into another, either to incorporate new features or resolve conflicts.
- Pull Request (PR): A request to merge changes from one branch into another with a code review.
Getting Started with Git
Installing Git
To start using Git, install it on your local system:
- Linux:
sudo apt-get install git - macOS:
brew install git - Windows: Download from Git’s website.
Configuring Git
Set up your name and email, which will be attached to your commits:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
To verify configurations:
git config --list
Git Essentials
Repositories
A repository (repo) is the core structure of any Git project. It includes all project files, a history of changes, and branch information.
Staging and Committing
- Staging: The process of adding files to the staging area to prepare them for a commit.
- Committing: Saves a snapshot of the staged changes in the repository with a unique identifier and message.
Basic workflow:
git add <filename> # Stage changes
git commit -m "Add description of changes" # Commit staged changes
Branches and Merging
Branches let you create separate environments for development, allowing you to work on features without affecting the main codebase. Merging incorporates changes from one branch into another.
git branch <branch-name> # Create a new branch
git checkout <branch-name> # Switch to that branch
git merge <branch-name> # Merge branch into current branch
Introduction to GitHub
What is GitHub?
GitHub is a cloud-based platform for Git repositories, providing tools for collaboration, code review, and project management.
Key Features of GitHub
- Pull Requests: Propose and discuss changes before merging.
- Issues: Track bugs and feature requests.
- GitHub Actions: Automate workflows for CI/CD.
- Project Boards: Visual task management using Kanban-style boards.
Working with Repositories on GitHub
- Creating a Repository: Go to GitHub and create a new repository for your project.
- Cloning a Repository: Copy a repository to your local machine with
git clone <repo-url>. - Syncing with Remote: Use
git pullto fetch and merge changes from the remote repo andgit pushto upload changes.
Git and GitHub Workflow
Forking and Cloning
Forking allows you to create a copy of another user’s repository, letting you make changes without affecting the original. Cloning copies a remote repository to your local machine.
Pull Requests and Code Reviews
- Opening a PR: After pushing changes to a branch, create a PR on GitHub to propose merging them into the main branch.
- Code Reviews: Collaborators can provide feedback, request changes, or approve the PR.
GitHub Issues and Project Management
GitHub offers Issues to track development tasks and Project Boards to organize these issues visually.
Git Commands in Depth
Working with Commits
- View Commit History:
git logshows a detailed history of commits. - Viewing Differences:
git diffdisplays changes between commits or branches. - Amending a Commit: Modify the last commit with
git commit --amend.
Branch Management
- List Branches:
git branchshows all branches. - Delete a Branch:
git branch -d <branch-name>removes a local branch. - Rename a Branch:
git branch -m <new-name>renames the current branch.
Merging Branches
Merging integrates changes from one branch to another. Use git merge <branch-name> to combine changes into the current branch.
Rebasing
Rebasing applies commits from one branch onto another, maintaining a linear history:
git rebase <branch-name>
Resetting and Reverting
- Resetting: Moves the repository to a previous state.
git reset --hard <commit-hash> # Discards all changes git reset --soft <commit-hash> # Keeps changes in staging - Reverting: Undoes a commit by creating a new commit.
git revert <commit-hash>
Stashing
Stashing lets you save changes temporarily without committing:
git stash # Save changes
git stash apply # Restore stashed changes
Cherry-Picking
Cherry-picking applies a specific commit from one branch onto another:
git cherry-pick <commit-hash>
Synchronizing with Remotes
- Fetch:
git fetchdownloads updates from the remote repository. - Pull:
git pullcombinesfetchandmerge. - Push:
git pushuploads local changes to the remote repository.
Advanced Git Techniques
Git Aliases
Aliases can simplify complex Git commands:
git config --global alias.st status
git config --global alias.cm commit
Interactive Rebase
Interactive rebasing allows you to reorder, squash, or edit commits:
git rebase -i <base-commit>
Common Mistakes and How to Avoid Them
Forgetting to Stage Changes
If you forget to git add your changes, they won’t be included in your commit. Always check with git status before committing.
Overwriting History with Rebase
Rebasing can rewrite history, so it’s recommended to avoid it on branches shared with others. Only rebase
branches that haven’t been pushed or that you’re working on independently.
Ignoring Merge Conflicts
When merging branches, conflicts may arise. Don’t skip or ignore these! Use a merge tool or manually resolve conflicts, and make sure to commit the resolved changes.
Accidental Commits to the Wrong Branch
Switching branches without committing or stashing changes can result in accidental commits. Always check your branch with git branch before committing and use git stash to save uncommitted changes if you need to switch.
Pushing Sensitive Information
Never commit sensitive data like passwords, API keys, or credentials. Use a .gitignore file to exclude sensitive files and directories:
echo "config.json" >> .gitignore
Force Pushing
Using git push --force can overwrite others' work. Only use --force when absolutely necessary, and communicate with your team beforehand. I have removed --force capabilitys on the github
Branching Structure and Naming Conventions
Purpose
This document establishes standards for creating, naming, and managing branches within our repository. Following these guidelines will ensure a clean, organized codebase, making it easier for the team to manage code changes, facilitate collaboration, and maintain clarity across multiple projects.
Table of Contents
Branch Structure
Our repository follows a structured branching system with two main branches, main and dev, along with various feature and ad-hoc branches off dev for specific tasks. All branches off dev should be named according to the conventions defined in this document.
1. main Branch
- Purpose: The
mainbranch contains production-ready code. This is where stable, tested code resides and reflects the latest release version of the codebase. - Access: Only project maintainers can directly merge code into
main. - Merges: Code merges into
mainshould always come fromdevafter passing code reviews and tests. - Protection: This branch is protected by branch rules, requiring code reviews and successful checks before merging.
2. dev Branch
- Purpose: The
devbranch is a staging branch for testing new features, bug fixes, and documentation updates before they reachmain. - Access: All contributors can create branches off
devfor their work. Contributors are not allowed to push directly todev; all changes must be submitted via pull requests. - Merges: All feature, bugfix, documentation, and test branches should be merged back into
devafter review and testing.
3. Feature and Ad-Hoc Branches
- Purpose: Branches off
devare used for specific features, bug fixes, testing updates, and documentation changes. These branches must follow the naming conventions detailed in the following section. - Lifecycle: Feature and ad-hoc branches are created for individual tasks and are short-lived. Once their purpose is completed, they are merged into
devand deleted. - Permissions: Contributors create, work on, and manage these branches off
devbut must submit pull requests (PRs) for all changes todev.
Note
: Only the GitHub repository manager may deviate from these conventions.
Branch Naming Conventions
To maintain organization and readability, all branches off dev must follow these naming conventions. Standard prefixes indicate the branch's purpose and scope, providing clarity to other contributors.
General Rules
- Lowercase Only: Branch names must be in lowercase.
- Hyphens for Separation: Use hyphens (
-) to separate words within a branch name. - Descriptive Names: Branch names should indicate the purpose of the branch (e.g., feature, bugfix) and include an identifier if relevant.
- Reference to Issue/Feature Numbers: When applicable, include a reference to the GitHub issue number.
Prefixes
| Prefix | Purpose | Example |
|---|---|---|
feature/ |
For new features or enhancements | feature/user-authentication |
bugfix/ |
For fixing known bugs or issues | bugfix/1023-login-error |
docs/ |
For documentation updates and additions | docs/api-endpoints-documentation |
test/ |
For changes or additions to tests | test/add-unit-tests |
hotfix/ |
For critical fixes that need immediate attention in dev or main |
hotfix/production-fix-login-error |
Note
: All prefixes indicate branches off
devand should not directly branch frommainwithout repository manager approval.
Branch Names, Scenarios and Examples
| Purpose | Branch Name | Description |
|---|---|---|
| New Feature | feature/user-registration |
Adds user registration functionality |
| Bug Fix | bugfix/215-api-authorization |
Fixes authorization issue on API requests |
| Documentation | docs/setup-instructions |
Adds setup instructions to documentation |
| Testing | test/integration-test-api |
Adds integration tests for API endpoints |
| Hotfix | hotfix/cache-issue |
Urgent fix for cache-related issue |
When working on specific aspects of the project, contributors should use designated branches to ensure consistency and organization. All branches, unless specified, must be created off the dev branch. Below are common scenarios and the appropriate branches for each task:
1. Adding a Web Scraper
- Branch:
DataCollection(branch offdev) - Process:
- Begin by switching to the
devbranch:git checkout dev git pull origin dev - Check out the
DataCollectionbranch:git checkout DataCollection - If
DataCollectiondoes not exist, create it offdev:git checkout -b DataCollection dev git push -u origin DataCollection
- Begin by switching to the
2. Adding Documentation
- Branch:
docs(branch offdev) - Process:
- Switch to the
devbranch and pull the latest updates:git checkout dev git pull origin dev - Check out the
docsbranch:git checkout docs - If
docsdoes not exist, create it offdev:git checkout -b docs dev git push -u origin docs
- Switch to the
3. Adding Business Files
- Branch:
businessfiles(branch offdev) - Process:
- Ensure you’re working from the latest
devupdates:git checkout dev git pull origin dev - Check out the
businessfilesbranch:git checkout businessfiles - If
businessfilesdoes not exist, create it offdev:git checkout -b businessfiles dev git push -u origin businessfiles
- Ensure you’re working from the latest
4. Bug Fixes
- Branch:
bugfix(branch offdev) - Process:
- Start from the latest
devupdates:git checkout dev git pull origin dev - Check out the
bugfixbranch:git checkout bugfix - If
bugfixdoes not exist, create it:git checkout -b bugfix dev git push -u origin bugfix
- Start from the latest
5. Hotfixes
- Branch:
hotfix(branch offdev) - Process:
- Ensure your local
devbranch is up to date:git checkout dev git pull origin dev - Check out the
hotfixbranch:git checkout hotfix - If
hotfixdoes not exist, create it:git checkout -b hotfix dev git push -u origin hotfix
- Ensure your local
6. Testing
- Branch:
testing(branch offdev) - Process:
- Start from the latest
devbranch:git checkout dev git pull origin dev - Check out the
testingbranch:git checkout testing - If
testingdoes not exist, create it:git checkout -b testing dev git push -u origin testing
- Start from the latest
7. Feature Development
- Branch:
feature/feature-name(branch offdev) - Process:
- Before starting, always check if a specific feature branch exists.
- If no specific branch exists, create a new feature branch off
dev:git checkout dev git pull origin dev git checkout -b feature/your-feature-name dev git push -u origin feature/your-feature-name - Note: Follow the naming conventions for all feature branches and verify it branches off
dev.
Handling Merge Conflicts
Merge conflicts can occur when changes in two branches affect the same lines of code. Here are best practices and commands to resolve them effectively.
Steps to Resolve Merge Conflicts
-
Identify the Conflict:
- When attempting to merge, Git will notify you of conflicts. Use the
git statuscommand to identify files with conflicts:git status
- When attempting to merge, Git will notify you of conflicts. Use the
-
Open and Review Conflicting Files:
- Open the conflicting files in an editor. Git marks conflicts with
<<<<<<<,=======, and>>>>>>>. - Manually review the changes, decide which version to keep, or combine changes where necessary.
- Open the conflicting files in an editor. Git marks conflicts with
-
Mark Conflicts as Resolved:
- After resolving conflicts in each file, mark it as resolved:
git add <file-name>
- After resolving conflicts in each file, mark it as resolved:
-
Complete the Merge:
- Once all conflicts are resolved, commit the merge:
git commit
- Once all conflicts are resolved, commit the merge:
-
Push the Resolved Branch:
- After committing, push the branch to the remote repository:
git push
- After committing, push the branch to the remote repository:
Best Practices for Conflict Resolution
- Resolve Locally: If possible, resolve conflicts on your local machine to minimize errors and ensure all changes are reviewed.
- Communicate with Team Members: If the conflict is complex or affects significant code, communicate with the involved team members for clarity and to avoid overwriting important changes.
- Use Git Tools for Assistance: Tools like
git diffandgit logare helpful for understanding changes in context. Additionally, many IDEs offer merge tools that visualize conflicts.
File Naming Standards
To maintain a consistent and organized structure, file names should follow these standards based on file type.
Documentation Files
- Capitalized Case: Use Capitalized Case for documentation files (e.g.,
GitAndGitHubStandards.md). - Avoid Special Characters: Do not use spaces or special characters.
- Location: Documentation related to each project should be stored within a
docs/ordocumentation/directory in the root of each project.
Code Files
- Lowercase with Underscores: Use lowercase names with underscores (e.g.,
user_authentication.py). - No Spaces or Special Characters: Avoid spaces or special characters in code file names.
- Descriptive Names: Name files according to their primary function or feature (e.g.,
data_processing.pyfor data processing functions).
Directory Naming
- Organization by Function: Directories should be organized by functionality (e.g.,
models/,controllers/,tests/). - Standard Directory Names: Keep directory names simple and standardized to make navigation intuitive.
Example Directory Structure:
project_root/
│
├── docs/
│ └── README.md
├── src/
│ ├── models/
│ ├── controllers/
│ ├── utils/
│ └── main.py
└── tests/
├── test_data_processing.py
└── test_user_authentication.py
Workflow for Contributors
This workflow ensures consistent practices across contributors for setting up branches, working locally, and pushing changes.
Step 0: Clone the Repository
Clone the repository to your local environment if you haven’t already.
git clone git@github.com:MiadTechnologiesLCC/MidasTechnologies.git
cd MidasTechnologies
Step 1: Set Up Branch Tracking
Ensure you have the latest branches set up locally:
git fetch origin
Step 2: Check Out dev for New Work
Always base new work off the dev branch:
git checkout dev
git pull origin dev # Ensure dev is up to date
Step 3: Create a New Branch Off dev
Create a new branch for your work based on the type of work you’re doing (feature, bugfix, docs, etc.). Follow the branch naming conventions.
git checkout -b feature/new-feature-name dev
Step 4: Make and Commit Changes
As you work, make regular commits with clear, descriptive messages following our commit message standards:
git add .
git commit -m "feat: add feature description"
Step 5: Push the Branch to GitHub
When ready, push your branch to GitHub:
git push -u origin feature/new-feature-name
Step 6: Create a Pull Request
- Create PR: Go to GitHub and create a pull request (PR) from your feature branch into
dev. - Review Request: Assign reviewers as necessary and respond to feedback.
- Ensure Compliance: Confirm that the PR adheres to all required tests, checks, and naming conventions before approval and merging.
Common Mistakes to Avoid
- Directly Branching Off
main: All branches should be created offdevunless given explicit permission from the repository manager. - Inconsistent Naming: Stick to prefix conventions and lowercase names with hyphens to maintain clarity.
- Forgetting to Pull
devUpdates: Always pull the latest changes fromdevto avoid conflicts. - Pushing to
devWithout a PR: Changes should never be pushed directly todev; submit all changes via a pull request. - Improper File Naming: Follow naming standards strictly, with documentation files in Capitalized Case and code files in lowercase with underscores.
Code Review Policy
Table of Contents
- Code Review Workflow
- Working with Pull Requests
- Code Review Focus Areas
- Reviewer Responsibilities
- Contributor Responsibilities
- Final Approval and Merging Process
- Common Code Review Mistakes to Avoid
- Post-Review Responsibilities
Code Review Workflow
Opening a Pull Request
When creating a Pull Request (PR), adhere to the following guidelines to ensure a smooth and productive review process:
-
Branch Source:
- PRs must originate from designated branches, such as feature, bugfix, hotfix, or docs, following the Branch Naming Conventions.
- PRs should generally target the
devbranch, while only production-ready PRs may target themainbranch. Merges tomainare restricted to project maintainers to maintain code quality.
-
Title & Description:
- Use a clear and concise title for each PR.
- Provide a thorough description, covering context, scope, and any relevant issues or implementation details.
- Clearly indicate any special requirements for deployment, breaking changes, or additional dependencies.
-
File Organization:
- Follow the # file-path standards to ensure all new files and directories are structured for efficient navigation and ease of maintenance.
Review Assignment and Timeline
- Assign PRs to experienced reviewers in relevant code areas. For complex changes, multiple reviewers may be required.
- Use GitHub tags to notify reviewers, aiming for a 24–48 hour turnaround on PR reviews to support an efficient workflow.
Working with Pull Requests
Create a PR for Each Feature
- Link the PR to any associated issues or tasks for traceability.
- Request a review from team members knowledgeable about the relevant functionality.
Resolve All Conversations Before Merging
- Address all reviewer feedback and mark conversations as resolved before final approval.
- If substantial modifications are made in response to feedback, re-request a review to ensure consensus.
Merge Protocol
- PRs into
dev: These require at least one reviewer’s approval. - PRs from
devtomain: Limited to the project maintainer, with thorough testing and final approval required to ensure the stability ofmain.
Code Review Focus Areas
Reviewers should concentrate on these critical areas to ensure quality, functionality, and maintainability across the codebase:
Code Quality and Consistency
- Follow our Coding Standards for readability, maintainability, and consistency.
- Maintain modularity and avoid redundancy, using existing functions or libraries where appropriate.
- Confirm adherence to language-specific standards (e.g., PEP8 for Python).
Functionality and Completeness
- Verify that the code performs as intended without regressions.
- Ensure all specified use cases, including edge cases, are covered. Seek clarification from the contributor if requirements are ambiguous.
Testing and Coverage
- Check for sufficient test coverage, ideally at least 85%, with a preference for automated tests.
- Ensure all tests, including unit, integration, and end-to-end (E2E) tests, pass successfully.
- Confirm compliance with the # file-path standards for consistency in test file organization.
Security and Performance
- Assess for potential security vulnerabilities, especially in areas related to data handling, user authentication, or API calls.
- Evaluate performance, suggesting improvements if any bottlenecks or inefficiencies are detected.
Documentation and Commenting
- All public functions, classes, and methods should have clear docstrings, following our Documentation Standards.
- Complex logic should be well-commented to explain non-standard approaches or optimizations.
- Verify updates to user or developer documentation when relevant.
File Structure and Organization
- Ensure new files and directories align with the # file-path standards to maintain logical structure.
- Check that file organization is consistent with the established hierarchy, supporting ease of access and maintenance.
Reviewer Responsibilities
Reviewers are accountable for providing timely, constructive, and actionable feedback:
- Timely Feedback: Complete PR reviews within 24–48 hours whenever possible.
- Constructive Feedback: Offer specific, actionable suggestions for improvements, avoiding vague criticism.
- Approval Process:
- Only approve PRs if they meet quality standards and have no outstanding issues.
- Request changes if additional work is needed or standards are not met.
- Ensure compliance with the # file-path standards, naming conventions, and coding guidelines.
- Professionalism: Maintain respect and clarity in all feedback, focusing on improvement.
Contributor Responsibilities
Contributors are responsible for ensuring their code is up to standard prior to submission:
- Self-Review: Conduct a thorough self-review, verifying clarity, standards compliance, and testing.
- Feedback Response: Address all reviewer comments and make necessary changes, re-requesting a review if major adjustments are made.
- Conversation Resolution: Mark all feedback conversations as resolved before requesting final approval.
- Commit Standards: Follow Commit Message Standards to keep commit messages informative, consistent, and concise.
Final Approval and Merging Process
Approval Requirements
- PRs must have at least one reviewer’s approval, with additional reviewers for complex or high-risk changes.
- Only project maintainers or designated personnel may approve and merge PRs into
main.
Merging to Dev or Main
- Merging into
dev: Contributors may merge PRs after at least one review and approval. - Merging into
main: Restricted to maintainers; requires extensive testing to confirm production readiness.
Final Checks Before Merging
- Ensure all tests pass and the latest code changes are integrated into the PR.
- Verify the # file-path standards are followed and the directory structure is organized.
- Update the PR description with any final notes or additional context.
Common Code Review Mistakes to Avoid
- Skipping Self-Review: Self-review reduces back-and-forth and helps identify issues preemptively.
- Insufficient Test Coverage: Lack of comprehensive tests can lead to unexpected bugs. Aim to cover all use cases and edge scenarios.
- Premature Approval: Ensure understanding of the PR’s purpose and functionality before approving.
- File and Path Non-Compliance: Files should adhere to naming and path standards as detailed in # file-path standards.
Post-Review Responsibilities
Continuous Improvement
- Identify opportunities for process improvement based on challenges encountered during the review.
- Record valuable lessons, significant changes, or patterns in a shared knowledge base for future use.
Follow-up Tasks
- Log any remaining issues or future improvements as GitHub issues for future sprints.
- Update documentation when significant changes impact user guides, API references, or other project docs.
Retrospective and Feedback
- Periodically evaluate the review process's effectiveness, gathering feedback to refine workflow, collaboration, and code quality practices.