Understanding Git in a Simple Way - Part 6
Source: Dev.to
When working on a feature branch
You may need to incorporate the latest changes from main. Rebasing lets you “replay” your branch’s commits on top of the updated main, resulting in a clean, linear history.
Create and switch to a new branch
git checkout -b new-feature
Make some commits
git add .
git commit -m "D"
Update the main branch
git checkout main
git pull origin main
Perform a rebase
git rebase main
Git takes the commits from your feature branch and re‑applies them onto the tip of main. After the rebase, the original commits become orphaned, and your branch has a linear history that includes the latest changes from main.
Handling conflicts
If conflicts arise during the rebase:
-
Resolve the conflicts in the affected files.
-
Stage the resolved files:
git add -
Continue the rebase:
git rebase --continue
You can abort the rebase at any time to return to the pre‑rebase state:
git rebase --abort
Git commands reference
| Command | Description |
|---|---|
git checkout -b | Creates and switches to a new branch. |
git rebase main | Moves your current branch’s commits to the tip of main. |
git rebase --continue | Continues the rebase after resolving conflicts. |
git rebase --abort | Stops the rebase and restores the branch to its original state. |