Branching in Git: A Guide to Managing Parallel Development

Learn how to effectively use branches in Git to manage parallel development and work on different features or versions of your project simultaneously. This guide will cover essential branching operations, such as creating, switching, merging, and deleting branches.



Git - Managing Branches

Introduction

Branch operations in Git allow for parallel lines of development. Developers can create branches to separate different features or versions of a project. For example, after releasing a product for version 6.0, you might create a branch for version 7.0 development, while keeping bug fixes for 6.0 in a separate branch.

Create Branch

Tom creates a new branch using the git branch <branch_name> command. By default, a branch starts from the current HEAD, but you can specify a different commit or tag.

Syntax

[jerry@CentOS src]$ git branch new_branch

[jerry@CentOS src]$ git branch
* master
  new_branch
        

The asterisk (*) indicates the currently checked-out branch.

Switch Between Branches

To switch branches, Jerry uses the git checkout command as shown below:

Syntax

[jerry@CentOS src]$ git checkout new_branch
Switched to branch 'new_branch'

[jerry@CentOS src]$ git branch
  master
* new_branch
        

Shortcut: Create and Switch Branch

Instead of using two commands to create and switch branches, Git provides the -b option with git checkout to create and switch in one step.

Syntax

[jerry@CentOS src]$ git checkout -b test_branch
Switched to a new branch 'test_branch'

[jerry@CentOS src]$ git branch
  master
  new_branch
* test_branch
        

Delete a Branch

To delete a branch, use the -D option with the git branch command. Before deleting, switch to another branch.

Syntax

[jerry@CentOS src]$ git checkout master
Switched to branch 'master'

[jerry@CentOS src]$ git branch -D test_branch
Deleted branch test_branch (was 5776472).

[jerry@CentOS src]$ git branch
* master
  new_branch
        

Rename a Branch

If the branch name is inappropriate, you can rename it using the -m option followed by the old and new branch names.

Syntax

[jerry@CentOS src]$ git branch -m new_branch wchar_support

[jerry@CentOS src]$ git branch
* master
  wchar_support
        

Merge Two Branches

Once Jerry finishes development on the wchar_support branch, he merges it with the master branch. The following commands show how to merge the two branches:

Syntax

[tom@CentOS project]$ git merge origin/wchar_support
Updating 5776472..64192f9
Fast-forward
src/string_operations.c | 10 ++++++++++
1 file changed, 10 insertions(+), 0 deletions(-)
        

Rebase Branches

The git rebase command allows developers to change the order of commits when merging branches. It modifies the history of the local branch by placing the commits from one branch on top of the other.

If a local branch has commits A→B→C→D and the merge branch has commits A→B→X→Y, git merge would result in A→B→C→D→X→Y. However, git rebase reorders the commits to A→B→X→Y→C→D.

Rebasing is helpful when multiple developers are working on the same repository and commit order needs to be maintained.