Git Branches: List, Create, Switch to, Merge, Push & Delete

Git Branches
Version Control
Web Development
Git Branches: List, Create, Switch to, Merge, Push, & Delete cover image

Introduction to Git branches

Branches in Git enable developers to work on different aspects of a project simultaneously without interfering with the main codebase. This functionality allows for efficient parallel development, experimentation with new features, and isolated bug fixing. By creating and managing branches, teams can ensure a smooth workflow and maintain the integrity of their project.

Captura de pantalla 2024-07-05 140914.png
The image above provides a visual representation of how Git branches operate within a repository. It illustrates multiple branches diverging from a common base, with each branch representing a separate line of development. Specifically, two branches are created: one for adding a new feature and another for fixing a bug. These branches allow developers to work on different tasks independently, ensuring that the new feature development and bug fixes do not interfere with the stability of the main codebase.

In Git, a branch is essentially a pointer to a specific commit, allowing developers to work on different features or fixes in isolation from the main codebase (often referred to as the "master" or "main" branch). This setup prevents unfinished or experimental code from impacting the stable version of the project. For instance, the image shows a main branch from which two feature branches have diverged. These feature branches enable developers to add new functionality or perform bug fixes independently and not affect the main branch. Once the development or bug fix is complete, branches can be merged back into the main branch.

In this article, we will explore the fundamental operations related to Git branches, including how to list, create, switch to, merge, push, and delete branches. Understanding these operations is essential for any developer looking to leverage the full potential of Git.

Git Commands

Git offers a range of commands to manage branches effectively. Below, we explain each command with examples:

  1. List branches

To list all the branches in your repository, use the git branch command. This will show all local branches, highlighting the current branch with an asterisk (*).

git branch

Example output:

* main



  1. Create a branch

To create a new branch, use the git branch <branch-name> command. This command creates a branch but does not switch to it.

git branch feature-branch

Alternatively, you can use git checkout -b <branch-name> to immediately create and switch to the new branch.

git checkout -b feature-branch

  1. Switch to a branch

To switch to an existing branch, use the git checkout <branch-name> command.

git checkout feature-branch

  1. Merge a Branch

To merge changes from one branch into another, switch to the branch you want to merge into, and then use the git merge <branch-name> command. The following commands merge the branch `feature-branch` into the `main` branch

git checkout main 

git merge feature-branch

git merge creates a new commit to perform the merge. It preserves history.

Captura de pantalla 2024-07-05 135950.png

Another strategy to perform the merge consists of using the command git rebase <branch-name>. Rebase works very similar to merge, except that it merges the branch by shifting it, so it does not preserve history

Captura de pantalla 2024-07-05 135727.png

  1. Push a branch

To push a local branch to a remote repository, use the git push origin <branch-name> command. This shares the branch with others who have access to the repository.

git push origin feature-branch

  1. Delete a branch

To delete a local branch that is no longer needed, use the git branch -d <branch-name> command. If the branch has not been merged, you can use the uppercase -d flag to force deletion.

git branch -d feature-branch

For deleting a remote branch, use:

git push origin --delete feature-branch

Git branches are critical for managing code changes in web development because they allow developers to work independently on features, bug fixes, and experiments. This guarantees efficient cooperation and a seamless workflow. The web development bootcamp at Code Labs Academy is designed to help students grasp Git and its branching strategies. Our hands-on projects and real-world scenarios equip students with knowledge and abilities that they can use right away. By concentrating on best practices in version control, our curriculum ensures that graduates are prepared to join professional development teams and confidently handle coding challenges.

Career Services background pattern

Career Services

Contact Section background image

Let’s stay in touch

Code Labs Academy © 2024 All rights reserved.