Branching



Overview



• Branching is required for isolating changes in situation such as amending someone else's code or testing new modules etc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Git Branch



• By convention, the main branch is typically named master
• What that does is to allow you to make changes in an area that won't affect the master branch


/ ---------- 0 ---------- 0 ---------- 0 feature branch
/ /
0 ---------- 0 ---------- 0 ---------- 0 ---------- 0 master branch (default)

0 = commit

Create a new branch



# Add a branch
git branch <branch name>
# or
# Add a bracnch and switch to <branch_name>
git checkout-b <branch_name> 


List All Branches



# List all branches
git branch


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Navigating Branches



# Swtich branch to <branch_name>
git checkout <branch_name>


• You can tell the branch is switched to <branch name> via the prompt

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Merge a Branch



• Branches are often used when implementing new features or hotfixes. They can be submitted for review by team members, and then once verified, can be pulled into the main codebase

Scenario 1

/ --------------- 0 feature branch
/ |
0 ---------- 0 ------- 0 ------ 0 master branch

Scenario 2

/ ------------ 0 --------\ feature branch
/ \
0 ---------- 0 ------- 0 ------- 0 ---- 0 master branch

• You need to checkout the master branch and perform a merge

# Merge branches
git merge <branch_name>

# Example
# Merge feature branch into master
git checkout <master branch>        # switch to master first
git merge <feature branch>


• The master branch now contains changes made in feature branch

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Remove a Branch



# Remove a branch
git branch -d <branch_name>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Dealing with Merge Conflicts



• At some point, you're going to have a merge conflict. This is where you may have made overlapping changes to a file, and git can't automatically merge the changes


Index