Welcome to the Git Usage Guideline for A2SV Projects. This comprehensive document will provide you with a clear and effective workflow for using Git within A2SV projects. By following these guidelines, you'll be able to collaborate seamlessly, manage version control, and contribute effectively to the project.
- Introduction
- Version Control with Git and GitHub
- Combining people's work on Git
- More on Git
- Pull Request
- Navigating a PR in Review
- Git FAQs
- Conclusion
This document serves as a comprehensive guide for using Git effectively within the A2SV projects. It aims to establish a consistent Git workflow, covering topics such as branching, making changes, committing, pushing, and creating pull requests. By adhering to these guidelines, you'll ensure that the codebase remains organized, maintainable, and scalable.
This section should give a comprehensive overview of version control using Git and GitHub, along with definitions and practical exercises to reinforce learning.
- Definition: A system that records changes to a file or set of files over time so you can recall specific versions later.
- Benefits:
- Tracks history of changes
- Collaboration with multiple contributors
- Backup and restore capabilities
- Definition: A distributed version control system that allows multiple people to work on a project simultaneously without interfering with each other’s changes.
- Key Features:
- Local repository for every user
- Fast performance
- Branching and merging capabilities
- Definition: A web-based platform that uses Git for version control and provides a collaborative environment for developers.
- Features:
- Repository hosting
- Collaboration tools (pull requests, issues)
- Code review and project management tools
- Repository: A storage location for your project's files and history.
- Stage: The area where changes are prepared for a commit.
- Commit: A recorded change to the repository.
- Push: Sending local commits to a remote repository.
- Pull: Fetching and integrating changes from a remote repository.
- Initialize or clone a repository to your local machine:
or
git init
git clone [repository-url]
- Make small, focused commits that address a single issue or feature.
- Stage changes:
git add [file-name]
- Commit changes with a meaningful message:
git commit -m "fix(AAiT-backend-1A): Update Login Page"
- Push changes to the remote branch:
git push
- Initialize and Clone a Repository:
git init
orgit clone <repository-url>
- Stage and Commit Changes:
- Make changes to a file, then
git add <file>
andgit commit -m "Message"
- Make changes to a file, then
- Push Changes to GitHub:
git push origin main
- Pull Changes from GitHub:
git pull origin main
- Using Git's features to manage and integrate changes from multiple contributors.
Branch: A separate line of development that allows you to work on a feature or fix without affecting the main codebase. It enables multiple developers to work on the same codebase without conflicting changes.
- Clone the repository to your local machine:
git clone https://github.com/bontu-fufa/git-tutorial.git
- Create a new branch for each feature or bug fix:
git branch [your-name].[task-name] git checkout [your-name].[task-name]
- Switch to a branch:
or
git checkout branch_name
git checkout -b [your-name].[task-name]
- Branch off from the main branch.
- Keep branches small and focused; delete them after they've served their purpose.
- Delete old branches.
- Checkout the branch you want to merge into (e.g., main):
git checkout main
- Pull any changes from the remote repository:
git pull
- Merge the other branch into the current branch:
git merge feature-branch
- Conflict: Occurs when two or more branches have made changes to the same code in a conflicting manner.
- Use
git status
andgit diff
to identify conflicts. - Resolve conflicts by editing the files manually and committing the changes.
- Use
git add
to stage the resolved files. - Use
git commit
to commit the resolved changes.
- Use
- Communicate with your team.
- Regularly merge changes from the main branch to avoid large conflicts.
- Use a consistent code style to minimize conflicts.
- Use
git merge --abort
to cancel a merge if conflicts are too difficult to resolve.
Rebase: The process of moving a branch to a new base commit, often used to combine multiple commits into a single one.
- Clone the repository to your local machine:
git clone https://github.com/bontu-fufa/git-tutorial-2.git
- Configure Git to rebase when pulling changes:
git config pull.rebase true [--global]
- Pull changes from the main branch:
git pull origin main
- Rebase:
git rebase <branch-name>
- Configure Git to always rebase when pulling changes:
git config pull.rebase true
- Git merge: Combines changes from one branch into another by creating a new commit.
- Git rebase: Integrates changes from one branch into another by replaying the changes on top of the destination branch, creating a linear history.
- Use Git merge to preserve the history of the source branch.
- Use Git rebase for a cleaner, linear project history.
- Stash: Temporarily save changes that are not ready to be committed, allowing you to switch branches or work on other tasks.
- Stash changes:
git stash
- List stashes:
git stash list
- Apply a stash:
git stash apply
- Delete a stash:
git stash drop
- Git Revert: Creates a new commit that is the opposite of an existing commit.
git revert <commit-id>
- Git Reset: Used to change the repository to a previous commit and discard any changes after that.
git reset <commit-id>
A Pull Request (PR) is a way to ask another developer to merge one of your branches into their repository. This not only makes it easier for project leads to keep track of changes, but also lets developers initiate discussions around their work before integrating it with the rest of the codebase.
-
Ensure your branch is up to date with the main branch:
git pull --rebase origin main
-
Create a pull request on GitHub:
- Navigate to your repository on GitHub and click the "New pull request" button.
- Select the branch you want to merge into the main branch.
- Add reviewers, a title, and a description for your pull request.
- Click "Create pull request" to submit your request.
- Wait for the pull request to be reviewed and approved by the reviewers.
-
Address any reviewer comments and make necessary changes.
-
Rebase your branch with changes from the main branch before merging:
git pull --rebase origin main
-
Merge the pull request using "Squash and Merge" to group smaller commits.
- The PR title should follow the commit convention, e.g.,
fix(AAiT-backend-1A): Update Login Page
. - Ensure your branch is up to date with the main branch.
- Changes should be fully tested and pass all tests.
- Commit messages should be meaningful and follow the commit guidelines.
- Use simple present tense. Example: "Update Login Page".
- Add a tag (refer to “conventional commit rules”). Example:
fix(mobile): Update Login Page
.
- The branch name should follow the pattern
[your-name].[task-name]
.
- Add a label when creating a pull request.
- The right size for a PR is one self-contained change. This usually involves just one part of a feature rather than an entire feature.
- Include related test code (new tests or updated tests).
- Provide all necessary context within the PR, its description, the existing codebase, or a previously reviewed PR.
- Ensure the system continues to function well for users and developers after the PR is merged.
- Quicker reviews
- Easier to merge
- More thorough reviews
- Acceptable when they involve deletions of whole files or are generated by trusted automatic refactoring tools.
- Reviewers may reject large changes and request smaller, incremental changes.
Code Review: ensures codebase consistency, maintainability, and improvement over time.
- Trade-offs: Balancing progress and code quality.
- Reviewer's Role: Ensure changes improve the codebase.
- "Better, Not Perfect": Prioritize continuous improvement.
- Ownership: Reviewer ensures code consistency.
- Approval Criteria: Changes should enhance system health.
- Design
- Functionality
- Complexity
- Tests
- Naming & Comments
- Style & Consistency
- Documentation
- Every Line
- Positive Aspects
- Practice PR: GitHub Practice PR
-
Take a Broad View of the Change:
- Review the PR description and overall purpose.
- Determine if the change is sensible and has a clear description.
-
Examine the Main Parts of the PR:
- Identify files with significant logical changes.
- Focus on these major parts first for context.
- Communicate any major design issues promptly.
-
Review the Remaining Files in an Appropriate Sequence:
- Establish a logical order for reviewing the remaining files.
- Check for related changes or potential impacts.
- Send comments highlighting design issues and suggesting improvements.
-
General Guidelines:
- Be kind and respectful.
- Explain your reasoning.
- Encourage simplification of code.
-
Label Comment Severity:
- Nit: Minor issue; should be addressed but isn't critical.
- Optional (or Consider): Good idea but not required.
- FYI: Not expected to be addressed in this change but useful for the future.
-
What if the developer is not responding to comments?
- Politely ask for a response.
- Escalate to the team lead or manager if necessary.
-
What if I don't understand a particular piece of code?
- Ask the developer for clarification.
-
What if I disagree with the author's implementation?
- Discuss and suggest alternatives.
-
How do I ensure that the code I'm reviewing is readable?
- Ensure clear naming, comments, and documentation.
-
What should I look for in terms of performance and efficiency?
- Check for optimized and efficient code.
-
What are some common code smells to look out for?
- Look for anti-patterns, overly complex code, and areas needing refactoring.
Refer to the Git FAQs section for answers to common Git-related questions.
Congratulations! You have completed the Git Workflow Guide for A2SV Projects. By following these best practices, you contribute to a well-organized, maintainable, and scalable codebase. If you encounter any questions or issues, don't hesitate to reach out to your team leader for assistance. Happy coding!
-
Fork the Repository:
- One student in each group of three will fork the repository.
- Navigate to the repository on GitHub and click the "Fork" button in the top right corner.
-
Invite Teammates to Collaborate:
- The student who forked the repository will invite their teammates to collaborate.
- Go to the forked repository on GitHub.
- Click on "Settings" > "Collaborators & teams" > "Invite a collaborator".
- Add the GitHub usernames of the teammates and send invitations.
-
Clone the Forked Repository:
- Each teammate will clone the forked repository.
git clone https://github.com/<your-username>/<forked-repo>.git
- Navigate into the cloned repository directory.
cd <forked-repo>
-
Ensure Your Branch is Up to Date with the Main Branch:
git pull --rebase origin main
-
Create a New Branch:
git checkout -b <new-branch-name>
-
Make Changes and Commit:
- Make the necessary changes in your local repository.
- Stage the changes.
git add .
- Commit the changes with a meaningful message.
git commit -m "Add meaningful commit message"
-
Push the Branch to GitHub:
git push origin <new-branch-name>
-
Create a Pull Request on GitHub:
- Navigate to the forked repository on GitHub.
- Click the "New pull request" button.
- Select the branch you want to merge into the main branch.
- Add reviewers and a title and description for your pull request.
- Click "Create pull request" to submit your request.
-
Address Reviewer Comments and Make Necessary Changes:
- Incorporate any feedback from reviewers and update the pull request as needed.
-
Rebase Your Branch with Changes from the Main Branch Before Merging:
git pull --rebase origin main
-
Merge the Pull Request Using "Squash and Merge" to Group Smaller Commits.