Git Rebasing
Rebasing is the process of moving or combining a sequence of commits to a new base commit. It rewrites the project history by creating brand new commits for each commit in the original branch, resulting in a linear project history.
Use Cases:
Examples:
1. Initialize repository:
git init
2. Create index.txt with:
My Website
git add .
git commit -m "Add title"
3. Add another commit to main. Edit index.txt to:
My Website
Welcome to my sit
git add .
git commit -m "Add welcome text"
4. Create feature branch:
git checkout -b feature-contact
5. Add commits to feature branch. Edit index.html to:
My Website
Welcome to my site
Contact Me
git add .
git commit -m "Add contact heading"
Edit index.txt to:
My Website
Welcome to my site
Contact Me
Email: me@example.com
git add .
git commit -m "Add email"
6. Go back to main and add more commits:
git checkout main
Edit index.txt to:
My Website
Welcome to my site
This site was built in 2026
git add .
git commit -m "Add footer"
7. Rebase feature branch onto main:
git checkout feature-contact
git rebase main
You'll get a conflict! Open index.txt and you'll see conflict markers with <<<<<<<, =======, and >>>>>>>
8. Fix the conflict by editing the file to keep both changes (remove conflict markers):
My Website
Welcome to my site
This site was built in 2026
Contact Me
Email: me@example.com
git add .
git rebase --continue
If there's another conflict, fix it the same way, then continue again. Done!
Commands for Rebasing:
Rebase current branch onto main:
git rebase main
Continue rebase after resolving conflicts:
git rebase --continue
Abort an in-progress rebase:
git rebase --abort
Skip current commit during rebase:
git rebase --skip