This guide will walk you through the process of working in a team where one person owns the repository, and all contributors create feature branches before pushing changes to the main branch. Additionally, we'll cover how to handle conflicts that may arise during the development process.
- Clone the Repository
- Create a Feature Branch
- Make Changes and Commit
- Push Changes to GitHub
- Create a Pull Request
- Review and Merge
- Handling Conflicts
First, clone the repository to your local machine:
git clone https://github.com/username/repository.git
Replace username/repository
with the actual username and repository name.
Before making changes, create a new branch for your feature:
git switch -c feature-branch
Replace feature-branch
with a descriptive name for your feature.
Make your changes to the code, add and commit them:
git add .
git commit -m "Description of changes"
Push your feature branch to GitHub:
git push origin feature-branch
On the GitHub repository page, create a pull request from your feature branch to the main branch. Provide details about the changes made.
The repository owner or a designated reviewer will review your pull request. If approved, they will merge it into the main branch.
Before creating a pull request or merging, ensure your local main branch is up-to-date:
git switch main
git pull origin main
If there are conflicts during the merge, Git will mark them in your files. Open the conflicted files, resolve the conflicts, and then add and commit the changes.
Push the resolved changes to GitHub:
git push origin main
Handling conflicts on GitHub involves resolving conflicting changes between branches, typically during the process of merging a pull request. Here's a more detailed explanation of handling conflicts directly on GitHub:
After pushing your feature branch, create a pull request on the GitHub repository.
Allow time for others to review your changes and address any feedback.
If there are conflicting changes between your branch and the main branch, GitHub will notify you. This happens when someone else has made changes to the same files you modified.
- Locate Conflicted Files: GitHub will mark the conflicted files in your pull request. Navigate to the "Files changed" tab to see the conflicting changes.
- Initiate Conflict Resolution: Click the "Resolve conflicts" button. This will take you to a file editor where you can manually resolve conflicts.
- Resolve Conflicts:
Manually edit the conflicting sections in the affected files. Remove the conflict markers (
<<<<<<<
,=======
,>>>>>>>
) and keep the desired changes. - Mark as Resolved: Once conflicts are resolved, mark the files as resolved.
- Commit Changes: Commit the changes directly on the branch. There's no need to create a new commit for conflict resolution.
- Rebase if Necessary: If there were conflicts, you might need to rebase your branch on the latest main branch to ensure a clean history.
- Merge Pull Request: Complete the pull request. If you resolved conflicts locally and pushed the changes, GitHub will recognize the resolution.
If the repository has continuous integration (CI) checks, wait for the checks to pass. CI systems often run tests to ensure the changes didn't introduce errors.
After merging, you can choose to delete the feature branch on GitHub.