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

Rebase Branch – Git Rebase

Rebase Branch – Git Rebase

Applying Changes with Git Rebase

git rebase is the command used to reapply all the commits made on the topic branch on top of the HEAD of the base branch (the branch that the topic branch was separated from). This command is useful when you want to streamline commits made on multiple branches. The rebase operation can also be done through a Remote Repository on the GitHub platform. It is an option of the merge feature of GitHub.

In the command line, you need to run the command on the branch to be rebased (a divergent branch) and specify its base branch name as a command parameter.

git rebase [base branch name]

The rebase command changes the divergence point to the HEAD of the base branch. It doesn't impact the base branch itself. If you want to integrate the changes into the base branch (e.g., the master branch), you need to run the git merge command on the base branch side. The merge action will be the Fast-Forward merge as the divergent branch is already ahead of the HEAD of the base branch.

For a better understanding, please go through the following practice section.

Practice

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

Objective:
Learn how to rebase a branch

1. Prepare a practice file

In the practice on the previous page, we merged Branch_A with the master branch using the squash option. For this practice purpose, we need to reverse the commit history before Branch_A was merged.

To reverse the HEAD of the master branch to commit M4, reset the merge operation by running the git reset --hard command on the master branch. Use the commit hash of commit M4 which is generated on your computer. Also, make sure to use the --hard option. If you don't use the option, the statuses of the Working Tree, INDEX, and HEAD will be mixed.

Command Line - INPUT
git checkout master
git reset --hard b7b73d4
git log --oneline

You'll see that the HEAD of the master branch is back to Commit M4.

Command Line - RESPONSE
b7b73d4 (HEAD -> master) M4
e12beda M3
f5ff7aa M2
8bf5c03 M1

Next, create commit M5 to match the illustration on the left of the main figure. Edit the HTML file (add <h1>M5</h1> and run the git commit command. To check the latest status, run the git log command again.

git_branch_practice.html
<!-- Master Branch-->
<h1>M1</h1>
<h1>M2</h1>
<h1>M3</h1>
<h1>M4</h1>
<h1>M5</h1>
<!-- /Master Branch-->
Command Line - INPUT
git commit -am "M5"
git log --oneline

Finally, you'll see the following status, which matches the illustration on the left of the main figure for the master branch.

Command Line - RESPONSE
288068f (HEAD -> master) M5
b7b73d4 M4
e12beda M3
f5ff7aa M2
8bf5c03 M1

Check the status of Branch_A.

Command Line - INPUT
git checkout Branch_A
git log --oneline

As we haven't touched Branch_A in this practice yet, its status remains the same as the one in the previous practice. Branch_A has diverged at commit M3 of the master branch, which is aligned with the original position of the Topic Branch on the illustration on the left of the main figure.

Command Line - RESPONSE
a4c4eb0 (HEAD -> Branch_A) A2
62dda63 A1
e12beda M3
f5ff7aa M2
8bf5c03 M1

2. Run the rebase command

Now you are ready to execute the rebase command. As you are already on Branch_A, run the following command.

Command Line - INPUT
git rebase master

You can see that Branch_A was successfully rebased.

Command Line - RESPONSE
Successfully rebased and updated refs/heads/Branch_A.

To check the latest status of Branch_A, run the git log command.

Command Line - INPUT
git log --oneline

Now you can see that Branch_A was rebased to the HEAD of the master branch (commit M5). In this operation, you can also confirm that the commit hashes for commits A1 and A2 are different from the original ones although the committed file versions remain the same.

Command Line - RESPONSE
479f994 (HEAD -> Branch_A) A2
6d96103 A1
288068f (master) M5
b7b73d4 M4
e12beda M3
f5ff7aa M2
8bf5c03 M1

3. Run the merge command

The rebase command changed the Branch_A status, however, the master branch stays the same; its HEAD is still at commit M5.

Command Line - INPUT
git checkout master
git log --oneline
Command Line - RESPONSE
288068f (HEAD -> master) M5
b7b73d4 M4
e12beda M3
f5ff7aa M2
8bf5c03 M1

If you want to integrate the change in Branch_A into the master branch, you need to run the merge command.

git merge Branch_A

In this case, the merge operation becomes Fast-forward as Branch_A was reattached to the HEAD of the master.

Command Line - RESPONSE
Updating 288068f..479f994
Fast-forward
 git_branch_practice.html | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

To check the latest status of the master branch, run the git log command.

Command Line - INPUT
git log --oneline

Now you can see that the HEAD of the master branch moved forward to the same position as the HEAD of Branch_A; the statuses of the master branch and Branch_A became the same.

Command Line - RESPONSE
479f994 (HEAD -> master, Branch_A) A2
6d96103 A1
288068f M5
b7b73d4 M4
e12beda M3
f5ff7aa M2
8bf5c03 M1

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

SSH Remote Login: Using Client-Generated Key Pair

SSH Remote Login (2) – Use Key Pair Generated by Client

Superuser vs. Normal User in Linux

Superuser (Root User) vs. Normal User

Using git status to Check Your Working Tree

Check Status of Working Tree and Staging Area – git status

Displaying Text with echo Command

echo (Echo input)

Building a Remote Collaboration Environment with Git & GitHub

Building Remote Collaboration Practice Environment

SSH Remote Login: Using Client-Generated Key Pair

SSH Remote Login (2) – Use Key Pair Generated by Client

Superuser vs. Normal User in Linux

Superuser (Root User) vs. Normal User

Using git status to Check Your Working Tree

Check Status of Working Tree and Staging Area – git status

Displaying Text with echo Command

echo (Echo input)

Building a Remote Collaboration Environment with Git & GitHub

Building Remote Collaboration Practice Environment

Tags:

Branch

Git Key Commands

Merge

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