Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Git & GitHub IntroductionChapter 5. Work With Branches

What Is Branch?

What Is Branch?

Understanding Branches in Git with Diagrams

Recap of the branch concept

As explained in Chapter 1, a branch in Git is an independent line of development with a commit history. Each branch provides a dedicated recording space and has its own coding history (a line of commits).

Branches allow us to manage different versions of the same set of project files simultaneously. For example, one developer can work on adding a new promotion campaign feature while another developer is working on adding a new payment feature to the same web application.

Git provides a master branch as a default. (If the repository was initiated on the GitHub platform, the default branch may be called the main branch. Check master branch vs. main branch.) Unless you create a new branch, all your work is done on the master branch. The branch to create a new feature is typically called a topic branch.

Once development is completed in a topic branch, you can merge it with the master branch or its parent branch.

With the branching functionality, you can efficiently collaborate with others. Or you can even utilize branches by yourself to manage versions to develop different features simultaneously.

Branch illustrations

In this chapter, we'll show how branches are recorded. The main figure shows two illustrations. Both illustrations have codes like M1, M2, etc. Each of them is a representation of one commit.

  • Tree diagram: The tree diagram is used to describe how branches diverge and integrate along coding work. This tree diagram is commonly used to get an overview of branches with a commit history.
  • Box illustration: The box illustration describes how branches store commits. A branch is like a file box for storing snapshots of coding history files. When a new commit is made, a snapshot of the project files for the commit is stored in the file box. Also, when one branch merges with another branch, the history of commits can be carried forward.

Branch tree diagram in the command line

You can confirm the branch tree status in the command line by running the git log command with the --oneline and --graph option.

Command Line - INPUT
git log --oneline --graph

The command line responses below are examples of the branch logs aligned with the illustrations. The seven-digit code in yellow is a commit hash (short version) and you can also see the status of each branch. You can compare the commit messages (M1, M2,..) with the illustration. As all branches are merged with the master branch, you can see all the branch statuses from the master branch. However, for Branch A, B, and C, you can see the commit histories which are related to their own histories.

Example 1: Master Branch's Commit History

You can see the histories of all the branches from the master branch as all the branches are already merged with the master branch.

* 98a7a09 (HEAD -> master, origin/master, origin/HEAD) M7
*   f44a7b1 M6
|\  
| *   55e0a1a (origin/Branch_B, Branch_B) B3
| |\  
| | * 089bade (origin/Branch_C, Branch_C) C1
| |/  
| * 3d7f400 B2
| * 42ba160 B1
* | c6835c2 M5
* |   fd694e3 M4
|\ \  
| |/  
|/|   
| * 59ca1dc (origin/Branch_A, Branch_A) A2
| * cb28ee8 A1
* | 43ca3d2 M3
|/  
* 5eb04c5 M2
* eadeda0 M1

Example 2: Branch A's Commit History

On the other hand, you have a limited view from Branch A. This is because all the changes after commit A2 were made on the other branches.

* 59ca1dc (HEAD, origin/Branch_A, Branch_A) A2
* cb28ee8 A1
* 5eb04c5 M2
* eadeda0 M1

Example 3: Branch B's Commit History

You can see that Branch C diverged from Branch B at commit B2 and merged with Branch B at commit B3.

*   55e0a1a (HEAD, origin/Branch_B, Branch_B) B3
|\  
| * 089bade (origin/Branch_C, Branch_C) C1
|/  
* 3d7f400 B2
* 42ba160 B1
* 43ca3d2 M3
* 5eb04c5 M2
* eadeda0 M1

Example 4: Branch C's Commit History

You can see that Branch C diverged from Branch B. However, the commit merging Branch C into Branch B is not visible from Branch C's status as the merge operation was done on Branch B. There is no impact on Branch C from the merge operation on Branch B.

* 089bade (HEAD, origin/Branch_C, Branch_C) C1
* 3d7f400 B2
* 42ba160 B1
* 43ca3d2 M3
* 5eb04c5 M2
* eadeda0 M1

The video below shows how different versions of code are displayed when you switch branches.

On the following topic pages, we'll explain how to manage branches in detail.

The demo code is available in this repository (Demo Code).

IdeaNote: Bring the demo code to your local computer

We'll explain how to run branch-related commands. Here are the heads-up in case you want to try them first.

You can clone the master branch and check the branch status by running the following commands.

Command Line - INPUT
git clone git@github.com:git-github-introduction/what-is-branch-demo.git
cd what-is-branch-demo
git branch -a

Then, check out all other branches.

Command Line - INPUT
git checkout Branch_A
git checkout Branch_B
git checkout Branch_C
git checkout master
git branch -a

You'll see that the four remote branches are now available on your local computer. We'll explain the branch concepts and commands in detail in this chapter.


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Introduction to Web Servers in Linux

Web Server

Chapter 6. Docker Compose and Deployment Practice

Chapter 6. Docker Compose and Deployment Practice

Managing User Passwords in Linux

passwd (Set Password)

Setting Up Gunicorn as Your Django Application Server

Application Server Setup – Gunicorn

Decision Making in Django App Deployment

Overview of Django App Deployment (2)

Introduction to Web Servers in Linux

Web Server

Chapter 6. Docker Compose and Deployment Practice

Chapter 6. Docker Compose and Deployment Practice

Managing User Passwords in Linux

passwd (Set Password)

Setting Up Gunicorn as Your Django Application Server

Application Server Setup – Gunicorn

Decision Making in Django App Deployment

Overview of Django App Deployment (2)

Tags:

Branch

Git & GitHub Introduction
Course Content

Chapter 1. Git & GitHub Overview

What Is Git?

What Is Version Control?

How To Save Versions in Git?

Collaborating on Git & GitHub – Remote Repository

Collaborating on Git & Git Hub – Branch

Git & GitHub Basic Life Cycle

Chapter 2. Git & GitHub Initial Settings

Git & GitHub Initial Settings Overview

Key Tool Preparation (1) – Mac

Key Tool Preparation (2) – Windows

Key Tool Preparation (3) – Linux Remote Server

Git User Settings – git config

Create GitHub Account

GitHub Access Authentication Settings

Generating PAT (Personal Access Token)

GitHub SSH Setup

Chapter 3. Git & GitHub Project Setup

Three Cases in Git & GitHub Project Setup

Git & GitHub Project Setup Overview in Different Cases

Building Remote Collaboration Practice Environment

Project Initiator – Key Steps To Launch Git Project

Project Initiator – Create Local Repository (git init)

Project Initiator – Make the First Commit

Project Initiator – .gitignore File

Project Initiator – Create Remote Repository

Project Initiator – Link Between Remote and Local Repositories (git remote add)

Project Initiator – Upload Local Repository to Remote Repository (git push)

Project Initiator – Grant Remote Repository Access to Project Members

Project Member – Start Project As Collaborator

Project Member – Create Copy of Project Code on Local Computer (git clone)

Non-Member – Start Project With Replica of Existing Repository (Fork)

Fork vs. Clone

Chapter 4. Edit & Commit

Git Regular Workflow – Edit & Commit

Edit and Commit Overview (1)

Add Files to Staging Area – git add

Commit Files – git commit

HEAD and INDEX

Check Status of Working Tree and Staging Area – git status

Check Commit Histories – git log

Check Differences – git diff

Restore Files to Working Tree – git restore

Undo Changes – git reset

Delete Files – git rm

Edit and Commit Overview (2)

Chapter 5. Work With Branches

Git Regular Workflow – Work With Branches

What Is Branch?

Branch Operation Basic Life Cycle

Create Branch and Check Branch Status – Git Branch

Switch Current Branch (1) – Git Checkout

Switch Current Branch (2) – Git Switch

Merge Branches – Git Merge

Fast-Forward Merge

Non-Fast-Forward Merge (No Option)

Non-Fast-Forward Merge (--no-ff Option)

Squash Merge

Rebase Branch – Git Rebase

Managing Conflict

Stash Changes – Git Stash

Chapter 6. Remote Collaboration

Git Regular Workflow – Remote Collaboration

Remote Collaboration Overview

Link With Remote Repository – Git Remote

Upload to Remote Repository – Git Push

Download Remote Repository and Merge to Local Repository – Git Pull

Get Remote Repository Information to Local Repository – Git Fetch

Pull vs. Fetch

Request for Review and Merge – Pull Request

Merge Operation Using GitHub

Chapter 7. Supplemental Topics

Git Key Commands and GitHub Key Features

Git & GitHub Glossary

GitHub Other Features

Chapter 1. Git & GitHub Overview

What Is Git?

What Is Version Control?

How To Save Versions in Git?

Collaborating on Git & GitHub – Remote Repository

Collaborating on Git & Git Hub – Branch

Git & GitHub Basic Life Cycle

Chapter 2. Git & GitHub Initial Settings

Git & GitHub Initial Settings Overview

Key Tool Preparation (1) – Mac

Key Tool Preparation (2) – Windows

Key Tool Preparation (3) – Linux Remote Server

Git User Settings – git config

Create GitHub Account

GitHub Access Authentication Settings

Generating PAT (Personal Access Token)

GitHub SSH Setup

Chapter 3. Git & GitHub Project Setup

Three Cases in Git & GitHub Project Setup

Git & GitHub Project Setup Overview in Different Cases

Building Remote Collaboration Practice Environment

Project Initiator – Key Steps To Launch Git Project

Project Initiator – Create Local Repository (git init)

Project Initiator – Make the First Commit

Project Initiator – .gitignore File

Project Initiator – Create Remote Repository

Project Initiator – Link Between Remote and Local Repositories (git remote add)

Project Initiator – Upload Local Repository to Remote Repository (git push)

Project Initiator – Grant Remote Repository Access to Project Members

Project Member – Start Project As Collaborator

Project Member – Create Copy of Project Code on Local Computer (git clone)

Non-Member – Start Project With Replica of Existing Repository (Fork)

Fork vs. Clone

Chapter 4. Edit & Commit

Git Regular Workflow – Edit & Commit

Edit and Commit Overview (1)

Add Files to Staging Area – git add

Commit Files – git commit

HEAD and INDEX

Check Status of Working Tree and Staging Area – git status

Check Commit Histories – git log

Check Differences – git diff

Restore Files to Working Tree – git restore

Undo Changes – git reset

Delete Files – git rm

Edit and Commit Overview (2)

Chapter 5. Work With Branches

Git Regular Workflow – Work With Branches

What Is Branch?

Branch Operation Basic Life Cycle

Create Branch and Check Branch Status – Git Branch

Switch Current Branch (1) – Git Checkout

Switch Current Branch (2) – Git Switch

Merge Branches – Git Merge

Fast-Forward Merge

Non-Fast-Forward Merge (No Option)

Non-Fast-Forward Merge (--no-ff Option)

Squash Merge

Rebase Branch – Git Rebase

Managing Conflict

Stash Changes – Git Stash

Chapter 6. Remote Collaboration

Git Regular Workflow – Remote Collaboration

Remote Collaboration Overview

Link With Remote Repository – Git Remote

Upload to Remote Repository – Git Push

Download Remote Repository and Merge to Local Repository – Git Pull

Get Remote Repository Information to Local Repository – Git Fetch

Pull vs. Fetch

Request for Review and Merge – Pull Request

Merge Operation Using GitHub

Chapter 7. Supplemental Topics

Git Key Commands and GitHub Key Features

Git & GitHub Glossary

GitHub Other Features