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:
- 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"
- Create and switch to a branch called
one-branch
:
git checkout -b one-branch
- 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"
- Switch to the
master
branch:
git checkout master
- Ensure that your local
master
branch is up to date with the remote repository:
git pull
- Rebase the
one-branch
onto the master
branch:
git rebase one-branch
- 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