Branching and Merging

Working with Branches

Branches allow you to work on different features or improvements without affecting the main code. This lesson covers creating branches, switching between them, and merging changes back together.

Key Concepts

Branches are separate lines of development that allow you to work on features independently. The main branch (often called master or main) contains stable code that is ready for production or competition. Feature branches isolate new work and prevent it from affecting the main codebase until it's ready. Merging combines changes from different branches back into the main branch. Branching enables parallel development where multiple team members can work on different features simultaneously.

Branch Operations

git branch feature-name          # Create new branch
git checkout branch-name        # Switch to branch
git checkout -b new-branch     # Create and switch
git branch                      # List branches
git branch -d branch-name      # Delete branch

Merging Workflows

When you're ready to combine your feature work with the main code, you merge the feature branch into main using git merge feature-branch

Robotics Branching Examples

For robotics teams, you might create an autonomous branch with git checkout -b autonomous-improvements, work on teleop features with git checkout -b teleop-enhancements, create a bug fix branch with git checkout -b fix-drivetrain-issue, merge working features with git merge autonomous-improvements, and keep the main branch for competition-ready code.

Conflict Resolution

Conflicts occur when branches modify the same lines in the same files. Git marks conflicts in affected files with special markers, and you must manually edit the files to resolve the conflicts. You can use git status to see which files have conflicts. Once you've resolved all conflicts, you complete the merge by adding the resolved files and committing the merge.

Understanding Branch Management

Effective branch management is crucial for team collaboration. The main branch should always contain stable, working code that's ready for competition or deployment. Feature branches should be short-lived and focused on single features or fixes to make them easier to review and merge. When merging, choose the appropriate strategy: fast-forward merges keep history linear, while merge commits preserve the branch structure. Regular cleanup of merged branches keeps your repository organized and prevents confusion about which branches are still active.

Further Reading Resources

Open full interactive app