- Scenario: You have a bug fix that needs to be applied to the main branch. Create a new branch to fix the bug and merge it into the main branch.
Code:
git checkout main
git pull
git checkout -b bugfix_branch
# Make changes to fix the bug
git add .
git commit -m "Fixed the bug"
git checkout main
git merge bugfix_branch
git push
- Scenario: You are working on a new feature that requires multiple commits. Create a new branch for the feature, make multiple commits, and then merge the branch into the main branch.
Code:
git checkout main
git pull
git checkout -b feature_branch
# Make changes and commit multiple times
git add .
git commit -m "Added feature"
# Make more changes and commit again
git add .
git commit -m "Updated feature"
git checkout main
git merge feature_branch
git push
- Scenario: You want to review changes made by another developer before merging them into the main branch. Create a pull request and review the changes.
Code:
git checkout main
git pull
git checkout -b feature_branch
# Make changes and commit
git add .
git commit -m "Added feature"
git push -u origin feature_branch
# Create a pull request on GitHub
# Review the changes and approve the pull request
# Merge the pull request into the main branch
- Scenario: You want to revert a commit that introduced a bug in the main branch. Revert the commit and create a new commit to fix the bug.
Code:
git checkout main
git pull
# Find the commit that introduced the bug
git log
git revert <commit_hash>
# Make changes to fix the bug
git add .
git commit -m "Fixed the bug introduced by <commit_hash>"
git push
- Scenario: You want to create a new release for your project. Create a release branch, make changes, and then merge it into the main branch.
Code:
git checkout main
git pull
git checkout -b release_branch
# Make changes for the new release
git add .
git commit -m "Added changes for release"
git checkout main
git merge release_branch
git push
These scenario-based questions can help you practice and become more comfortable with Git and GitHub branching strategy. Remember to use best practices and follow the standard Git workflow to ensure efficient collaboration and high code quality.