Git & GitHub / Git Branching and Merging

Working with Branches in Git

In this tutorial, you'll learn how to create, switch, and delete branches in Git. These are fundamental skills for any developer working with Git.

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Covers creating branches, merging changes, and resolving conflicts in Git.

1. Introduction

This tutorial is designed to guide you through the process of creating, switching, and deleting branches in Git, a crucial skill for any software developer. By the end of this tutorial, you will be able to manage your Git branches effectively to streamline your workflow and collaborate more efficiently.

Prerequisites: Basic understanding of Git and version control.

2. Step-by-Step Guide

A branch in Git is essentially a unique set of code changes with a unique name. The default branch name in Git is "master". When you make changes, you're actually making changes to your own branch, not the master branch.

To create a new branch:

git branch <branch-name>

To switch to a different branch:

git checkout <branch-name>

To delete a branch:

git branch -d <branch-name>

Note: You can't delete a branch if you're currently on it. You must switch to another branch before deleting.

3. Code Examples

Creating a new branch and switching to it:

# Create a new branch named 'new-feature'
git branch new-feature

# Switch to the 'new-feature' branch
git checkout new-feature

Deleting a branch:

# Switch back to the master branch
git checkout master

# Delete the 'new-feature' branch
git branch -d new-feature

4. Summary

In this tutorial, we've learned how to create, switch, and delete branches in Git. This is a fundamental skill for any developer and is essential for efficient collaboration and version control.

For further learning, explore merging branches and resolving conflicts in Git. The official Git documentation is a valuable resource for this.

5. Practice Exercises

  1. Create a new branch named 'test-branch', switch to it, create a new file, add some content, and commit it.

  2. Solution:
    ```bash
    # Create and switch to a new branch named 'test-branch'
    git checkout -b test-branch

    # Create a new file
    echo "This is a test file." > test.txt

    # Add the new file to the staging area
    git add test.txt

    # Commit your changes
    git commit -m "Add test file"
    ```

  3. Switch back to the master branch and try to delete the 'test-branch'. What happens and why?

  4. Solution: You can't delete a branch that has unmerged changes. Git prevents you from losing those changes.

  5. Merge 'test-branch' into the master branch and then delete 'test-branch'.

  6. Solution:
    ```bash
    # Switch to the master branch
    git checkout master

    # Merge 'test-branch' into the master branch
    git merge test-branch

    # Delete 'test-branch'
    git branch -d test-branch
    ```

Remember to keep practicing these commands until you're comfortable with them. They are core to your workflow in Git.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

File Size Checker

Check the size of uploaded files.

Use tool

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

Backlink Checker

Analyze and validate backlinks.

Use tool

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help