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

Create Branch and Check Branch Status – Git Branch

Create Branch and Check Branch Status – Git Branch

Creating and Managing Branches with git branch

git branch is a multi-use command. We'll explain the three major use cases on this page.

The git branch command use cases

  1. Create a new branch
  2. Check branch status
  3. Delete an unused existing branch

1. Create a new branch

Creating a new branch is the first action in the branch operation. When running git branch [new branch name], a new branch is created from the original branch where you are located.

However, you are still on the original branch until you switch the current branch to the newly created branch by running the git checkout or git switch commands, which will be explained on the next pages.

Also, one important note here is that a new branch diverges from the latest commit (HEAD) of the parent branch. Unless you rebase it (to be explained later), the diverged point remains the same throughout the entire life cycle of the branch.

Example

If you want to create a new branch named Branch_A, run the command below.

git branch Branch_A

The image below illustrates an example of how Branch_A is created from the master branch.

Git branch visual explanation

2. Check branch status

When you simply run the git branch command without any additions, the command line returns the branch status. The branch status includes a list of branches and the information about which branch you are located in.

Example

In the following example, there are two branches: the master branch and Branch_A. The branch with " * " is the current branch, which is the master branch in this case.

Command Line - INPUT
git branch
Command Line - RESPONSE
  Branch_A
* master

The git branch command with the -a option returns the list of branches including branch information in the Remote Repository recorded in the Local Repository if the Local Repository is already linked with the Remote Repository.

Example

Command Line - INPUT
git branch -a
Command Line - RESPONSE
  Branch_A
* master
  remotes/origin/master

How to manage branches in the Remote Repository will be explained in the next chapter.

3. Delete unused branch

When you don't need a branch after merging it with another branch, you can delete the branch by running the git branch -d [existing branch name] command.

Example

Command Line - INPUT
git branch -d Branch_A
Command Line - RESPONSE
Deleted branch Branch_A (was 86c37e9).

If the branch you are trying to delete has not been merged with any other branches, the command line gives you an error.

Command Line - RESPONSE
error: The branch 'Branch_A' is not fully merged.
If you are sure you want to delete it, run 'git branch -D Branch_A'.

If you really want to delete the branch (for example, when you mistakenly made a new branch), you can use the -D option. git branch -D [existing branch name] allows you to delete a branch regardless of its merge history.

Example

Command Line - INPUT
git branch -D Branch_A
Command Line - RESPONSE
Deleted branch Branch_A (was b43edc9).

4. Rename branch

There are other usages of the git branch command. For example, if you want to change a branch name, you can use the -m option. Switch to the branch you want to rename. Then, run the git branch -m [new branch name] command.

Example

To change the Branch_A's branch name to Branch_B, run the command below on Branch_A.

Command Line - INPUT
git branch -m Branch_B

Unless there is an error, the command line won't give you a response. To check if the branch name is successfully changed, run the git branch command.

Example

Command Line - INPUT
git branch
Command Line - RESPONSE
* Branch_B
  master

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Introduction to SSH in Linux

SSH (Secure Shell)

Viewing Commit Histories with git log

Check Commit Histories – git log

Defining Django Views: Function vs. Class-Based

Create Views

Linux Signals

Signals

Simplifying SCP with SSH Config File

SCP with SSH Config File

Introduction to SSH in Linux

SSH (Secure Shell)

Viewing Commit Histories with git log

Check Commit Histories – git log

Defining Django Views: Function vs. Class-Based

Create Views

Linux Signals

Signals

Simplifying SCP with SSH Config File

SCP with SSH Config File

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