Rebase onto Another Branch

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Git is a powerful version control system that allows developers to collaborate on projects efficiently. One of the most useful features of Git is the ability to rebase a branch onto another branch. This allows developers to incorporate changes from one branch into another while maintaining a clean and linear history.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BranchManagementGroup(["`Branch Management`"]) git/BranchManagementGroup -.-> git/branch("`Handle Branches`") subgraph Lab Skills git/branch -.-> lab-12749{{"`Rebase onto Another Branch`"}} end

Rebase onto Another Branch

As a developer, you are working on a project with multiple branches. You have made changes to your branch and want to incorporate those changes into another branch. However, you don't want to merge the branches because you want to maintain a clean and linear history. In this case, you can use the git rebase command to rebase your branch onto another branch.

For this lab, let's use the repository from https://github.com/labex-labs/git-playground. Follow the steps below to complete the lab:

  1. Clone the repository, navigate to the directory and configure the identity:
git clone https://github.com/labex-labs/git-playground
cd git-playground
git config --global user.name "your-username"
git config --global user.email "your-email"
  1. Create and switch to a branch called one-branch:
git checkout -b one-branch
  1. Add "hello,world" to the README.md file, add it to the staging area and commit it with the message "Added some changes to README.md":
echo "hello,world" >> README.md
git add .
git commit -am "Added some changes to README.md"
  1. Switch to the master branch:
git checkout master
  1. Ensure that your local master branch is up to date with the remote repository:
git pull
  1. Rebase the one-branch onto the master branch:
git rebase one-branch
  1. Resolve any conflicts that arise during the rebase process.

This is the result of running git log:

commit eccff423dd6bf5335f76f2f364fa3b95130ff805 (HEAD -> master, one-branch)
Author: xiaoshengyunan <@users.noreply.github.com>
Date:   Sat Jul 22 23:10:04 2023 +0800

    Added some changes to README.md

Summary

Rebasing a branch onto another branch is a powerful feature of Git that allows developers to incorporate changes from one branch into another while maintaining a clean and linear history. By completing this lab, you have learned how to use the git rebase command to rebase a branch onto another branch.

Other Git Tutorials you may like