Table of Contents
When should we create a new branch?
Branches are used to diverge from the main code base. They are useful because they create a copy of existing code without modifying the existing code. Think of it as your very own sandbox where you can create anything new.
Therefore, a new branch should be created for any new change to any of the files in the project. This includes but is not limited to creating a new feature in the repo and/or fixing a bug in the repo.
When do merge conflicts occur?
Merge conflicts occur when we have code that could possibly overwrite code that was already there. They are bound to happen if multiple people are working on the same file.
Things to avoid
The main branch should always have working code so as a best practice, we shouldn't
- Work off of the
main
branch. - Merge code that hasn't been tested into the
main
branch .
Each team member should own their own branch and work exclusively on that branch.
First, create a branch and switch to it:
git checkout -b ben-feature-A
Then see all branches:
git branch
You should then see: (the *
indicates the current branch)
main
* ben-feature-A
TIP: Always check to make sure that you are NOT working in the
main
branch
To switch back and forth between main
and your branch, run:
git checkout main
git checkout ben-feature-A
When you're ready to save your current changes, create a local commit.
In your feature branch, run:
git add -A
git commit -m 'a message describing your changes'
While you have been working on your code, your teammates may have pushed changes.
First, switch to the main
branch and pull
the changes:
git checkout main
git pull
Then, switch back to your feature branch and merge the changes from main
into your branch.
git checkout ben-feature-A
git merge main
You may need to resolve merge conflicts at this point.
If you see this screen asking you to enter a commit message, simply type :q to quit and accept the default merge message.
Once you have merged main
into your branch, go ahead and git push
.
If it is your first time pushing from this branch, you will be told to use the
--set-upstream
flag.
- Go to Github.com and open up your repository.
- Then, click on the Pull Requests tab to create a new pull request to merge your branch into
main
. - Ask your teammates to review your code and then merge!
- If you want to continue working on your branch, do NOT delete the branch.
Your teammates can then follow the steps listed in merge before you push to update their local repositories.