Git Branch for beginners

Joshua Wilfred
2 min readMay 11, 2023

--

Git is a powerful version control system that allows developers to easily manage changes to their codebase. One of the key features of Git is its ability to create branches, which are separate lines of development that can be merged back into the main codebase when they’re ready.

In this blog post, we’ll walk you through the process of creating a branch, switching between branches, and merging branches in Git.

Creating a branch

To create a new branch in Git, you’ll need to use the command line interface (CLI) or terminal. First, navigate to your local Git repository in your CLI. Then, type the following command to create a new branch called “develop”:

git branch develop

This will create a new branch in your Git repository called “develop”. To switch to this new branch, type the following command:

git checkout develop

Alternatively, you can combine the branch creation and checkout steps into one command:

git checkout -b develop

This command will create a new branch called “develop” and switch to it in one step.

Switching between branches

Once you’ve created multiple branches in your Git repository, you’ll need to switch between them to work on different features or bug fixes. To switch to a different branch, type the following command:

git checkout <branch-name>

Replace `<branch-name>` with the name of the branch you want to switch to. For example, to switch to the “master” branch, you would type:

git checkout master

To see a list of all the branches in your local repository, type the following command:

git branch

This will display a list of all the branches in your repository, with an asterisk next to the branch you’re currently on.

Merging branches

Once you’ve made changes to a branch and are ready to merge it back into the main codebase, you’ll need to follow a few steps to ensure a smooth merge. Here’s how to do it:

1. Before merging, ensure that the branch you want to merge is up-to-date with the latest changes from the branch you want to merge it into. To update the branch, switch to the branch you want to update and type `git pull`.

2. Switch to the branch you want to merge into. For example, if you want to merge the “develop” branch into the “master” branch, you would type:

git checkout master

3. Type the following command to merge the branch named `<branch-name>` into your current branch:

git merge <branch-name>

Replace `<branch-name>` with the name of the branch you want to merge. For example, to merge the “develop” branch into the “master” branch, you would type:

git merge develop

4. If there are any conflicts between the two branches, Git will notify you and prompt you to resolve the conflicts manually. Once the conflicts are resolved, you can commit the changes and complete the merge.

And that’s it! With these steps, you should be able to create new branches, switch between them, and merge them as needed. :)

--

--