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

Switch Current Branch (1) – Git Checkout

Switch Current Branch (1) – Git Checkout

How to Switch Branches Using git checkout

The git checkout command is used for switching your current branch to a selected branch. You also create a new branch with the -b option and immediately switch to the new branch. When you switch your current branch with changes in INDEX and/or the Working Tree, you need to carefully manage the operation as the changes may cause a problem.

Switch the current branch

As explained on the previous page, the git branch [new branch name] creates a new branch but the current branch location stays the same. git checkout is used when you want to switch your branch to a selected branch.

Example

To switch to Branch_A, run the command below.

Command Line - INPUT
git checkout Branch_A
Command Line - RESPONSE
Switched to branch 'Branch_A'

The image below illustrates an example of how the command above works.

git checkout: switch to new branch

Create a new branch and switch to the new branch

When you use this command with the -b option, you can create a new branch and switch to the new branch at the same time.

Example

To create a new branch named Branch_A and switch to the new branch at the same time, run the command below.

Command Line - INPUT
git checkout -b Branch_A
Command Line - RESPONSE
Switched to a new branch 'Branch_A'

The image below illustrates an example of how the command above works.

git checkout -b: create a new branch and switch to the new branch

Checkout when you have changes in INDEX and/or the working tree

When you checkout to another branch, the Git system tries to carry the changes in the Working Tree and INDEX over to the destination branch. If the latest commit statuses of both branches are the same, you can carry over the Working Tree and INDEX (Situation A).

Checkout when you have changes in INDEX and/or the working tree: Situation A

However, if one of the branches has already evolved, you cannot switch to the destination branch (Situation B) ...

Checkout when you have changes in INDEX and/or the working tree: Situation B

... and you'll see an error message like below.

Command Line - RESPONSE
error: Your local changes to the following files would be overwritten by checkout:
        git_practice.html
Please commit your changes or stash them before you switch branches.
Aborting

Solutions

There are some options to solve this issue.

Option 1. Commit all the changes and make the INDEX, Working Tree, and HEAD the same

Option 2. Stash the changes (refer to Stash Changes – Git Stash)

Option 3. Forcefully switch branches by running the git checkout command with the -f option

When you use the -f option, you need to be very careful as the INDEX and changes in the Working Tree are all cleared.

Practice

bloovee-round-icon.pngDeveloper A (Project Owner Role)

Objective:
Create, switch, and delete branches

1. Setup a practice project directory and file for this chapter

Directory and file structure

For this practice, we'll use the following directory and file. The directory and file will be used throughout the practices in this chapter except for the last topic.

  • Practice project directory: git_branch_practice
  • Practice file: git_branch_practice.html

The screenshot below is the target directory structure example based on Mac OS.

git project directory structure example

Create the practice project directory and file

Open the project's main directory (e.g., Dev_A_bloovee) with VS Code. You can use drag & drop to open the directory.

Open a project folder in VS Code with drag and drop

After opening the project's main directory with VS Code, open a new terminal in the VS Code window. The main project directory is already set as the current working directory in the command line.

You can also use the command line to move the current directory to the main project directory.

Command Line - INPUT
cd ~/Dev_A_bloovee

Once the current working directory is properly set, run the command below to create the directory and file.

Command Line - INPUT
mkdir git_branch_practice
cd git_branch_practice
touch git_branch_practice.html

Edit the practice HTML file

Open the git_branch_practice.html file with a text editor and make the following edits.

git_branch_practice.html (master)
<!doctype html>
<html lang="en">
<body>
<!-- Master Branch-->
<h1>M1</h1>
<!-- /Master Branch-->

<!-- Branch A-->
N/A
<!-- /Branch A-->

<!-- Branch B-->
N/A
<!-- /Branch B-->
</body>

For easier understanding, we'll add text for each branch in different lines of code dedicated to each branch. <!-- --> is used for making comments for html files. In the above case, we created three writing areas for each branch: master, Branch_A, and Branch_B. At this stage, only the master branch exists. For the master branch section, write "M1" under <h1> tag that will be the same as the commit message which we will create later on.

Create a Local Repository and make the first commit

After saving the file, commit the file with "M1" as a commit message. Run the following command to execute.

As we haven't initiated Git in the new project directory, run the git init command first. Add the files under the project directory to INDEX (in this case, only one file) and commit them. To check the commit status, run the git log command.

Command Line - INPUT
git init
git add .
git commit -m "M1"
git log --oneline

You can see that a new commit "M1" is created. (On Windows, you may not see the hint messages.)

Command Line - RESPONSE
hint: Using 'master' as the name for the initial branch. This default branch name
:
hint:   git branch -m <name>
Initialized empty Git repository in /Users/bloovee/Dev_A_bloovee/git_branch_practice/.git/
[master (root-commit) 8bf5c03] M1
 1 file changed, 15 insertions(+)
 create mode 100644 git_branch_practice.html
8bf5c03 (HEAD -> master) M1

Note: This approach is only for practice purposes. In actual projects, you need to use a concise description of the key changes in the code (e.g., "fixed landing page UI", "integrated APIs", etc.).

2. Create a new branch

Create Branch_A by running the git branch Branch_A command followed by the git branch to check the branch status.

Command Line - INPUT
git branch Branch_A
git branch

You can see that a new branch Branch_A has been created already, however, the current branch is still the master branch (the branch with asterisk " * " is the current branch):

Command Line - RESPONSE
  Branch_A
* master

3. Switch to the new branch

To make a new edit on the new branch, switch the current branch to Branch_A by running the git checkout command (you can also use the git switch command). Also, run the git branch command to confirm that the current branch has been switched to Branch_A.

Command Line - INPUT
git checkout Branch_A
git branch

You can see that the current branch has been switched to Branch_A:

Command Line - RESPONSE
Switched to branch 'Branch_A'
* Branch_A
  master

4. Edit and commit on the new branch (Branch_A)

To make another version of the project file, edit the git_branch_practice.html file. At this time, change only the Branch_A section of the file. Change N/A to A1, which will be the same as the first commit message on this branch.

git_branch_practice.html (Branch_A)
<!-- Branch A-->
<h1>A1</h1>
<!-- /Branch A-->

After saving the file, commit the file and check the log.

Command Line - INPUT
git commit -am "A1"
git log --oneline

You can see that a new commit has been created on Branch_A.

Command Line - RESPONSE
[Branch_A de11d0c] A1
 1 file changed, 1 insertion(+), 1 deletion(-)
de11d0c (HEAD -> Branch_A) A1
8bf5c03 (master) M1

5. Create a new branch and switch to the new branch with a shortcut

Go back to the master branch first to create another new branch from the master branch. To create Branch_B and switch to the branch using only one command, run git checkout -b Branch_B (or run git switch -c Branch_B). And check the branch status.

Command Line - INPUT
git checkout master
git checkout -b Branch_B
git branch

You can see that Branch_B has been created and it became the current branch.

Command Line - RESPONSE
Switched to branch 'master'
Switched to a new branch 'Branch_B'
  Branch_A
* Branch_B
  master

6. Edit and commit on the new branch (Branch_B)

To make another version of the project file, edit the git_branch_practice.html file again. This time, change only the Branch_B section of the file. Change N/A to B1, which will be the same as the first commit message on this branch.

git_branch_practice.html (Branch_B)
<!-- Branch B-->
<h1>B1</h1>
<!-- /Branch B-->

After saving the file, commit the file with a message of "B1" and check the log.

Command Line - INPUT
git commit -am "B1"
git log --oneline

You can see that a new commit was created on Branch_B. Although we have made a commit on Branch_A, we cannot see it in this log. This is because Branch_B diverged from the master branch (not from Branch_A).

Command Line - RESPONSE
[Branch_B 5536835] B1
 1 file changed, 1 insertion(+), 1 deletion(-)
5536835 (HEAD -> Branch_B) B1
8bf5c03 (master) M1

7. Forcefully switch to another branch

In the latest situation, you can switch to another branch as all changes are already committed (the Working Tree and INDEX are the same as HEAD). To create a situation where you cannot switch to another branch like in the case explained on the previous page, edit the practice file again and save it like shown below.

git_branch_practice.html (Branch_B)
<!-- Branch B-->
<h1>B1</h1>
<h1>B2</h1>
<!-- /Branch B-->

At this stage, the above edit is only reflected in the Working Tree and is not committed. Try to switch to Branch_A with the following command.

Command Line - INPUT
git checkout Branch_A

In this case, you'll see an error message like the one below.

Command Line - RESPONSE
error: Your local changes to the following files would be overwritten by checkout:
        git_branch_practice.html
Please commit your changes or stash them before you switch branches.
Aborting

Now try to switch to Branch_A with the "-f" option and check the branch status.

Command Line - INPUT
git checkout -f Branch_A
git branch

This time, you successfully switched to Branch_A, however, the changes done in the Working Tree are cleared by this command. You can check the status in your file.

Command Line - RESPONSE
Switched to branch 'Branch_A'
* Branch_A
  Branch_B
  master

The Branch B section is back to its original status.

git_branch_practice.html (Branch_A)
<!-- Branch B-->
N/A
<!-- /Branch B-->

8. Delete a branch

Lastly, delete Branch_B. As the branch has not been merged yet, so the "-d" option won't work. Use the "-D" option instead. Run the following commands and check the branch status.

Command Line - INPUT
git branch -D Branch_B
git branch

You can see that Branch_B has already been erased.

Command Line - RESPONSE
Deleted branch Branch_B (was 5536835).
* Branch_A
  master

For the next practice, let's also delete Branch_A by running the following commands.

Command Line - INPUT
git checkout master
git branch -D Branch_A
git branch

You can see that only the master branch exists.

Command Line - RESPONSE
Switched to branch 'master'
Deleted branch Branch_A (was de11d0c).
* master

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Testing Django App UI on Mobile Devices

Check Developing App UI on Mobile Device

Absolute vs Relative Paths in Linux

Absolute Path and Relative Path (Linux OS)

Customizing Django Allauth Templates

Django Allauth (7) – Allauth Template File Setup

Configuring Git & GitHub Settings

Chapter 2. Git & GitHub Initial Settings

Exploring User Groups in Linux

Group – Primary Group and Secondary Group

Testing Django App UI on Mobile Devices

Check Developing App UI on Mobile Device

Absolute vs Relative Paths in Linux

Absolute Path and Relative Path (Linux OS)

Customizing Django Allauth Templates

Django Allauth (7) – Allauth Template File Setup

Configuring Git & GitHub Settings

Chapter 2. Git & GitHub Initial Settings

Exploring User Groups in Linux

Group – Primary Group and Secondary Group

Tags:

Branch

Git Key Commands

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