Below is a step-by-step guide (in plain English) for rebasing your feature branch on top of main. The code is purely demonstrative—treat it as a starting point if you want an automated workflow around Git operations.
Tip
There is an attached bash script I call gitru.sh
(git rebase update). It automatically rebases your local feature branch (basically it updates your local branch with changes from the main
branch). Only run it on separate feature branches avoid running it on the main
branch.
- You have a feature branch that was originally created off the main branch.
- The main branch has received new commits, and you need your feature branch to include those changes.
- You prefer a rebase so your feature branch’s commits appear on top of the main branch commits, creating a linear history.
- A command-line interface (CLI) with Git installed.
- Access to the Git repository containing both
main
andfeature
branches. - (Optionally) A Deno environment if you intend to automate any steps with TypeScript scripts.
- Ensure your local
main
is up to date:git checkout main git pull origin main
- Switch to your feature branch:
git checkout feature
- Start the rebase onto
main
:git rebase main
- Resolve conflicts (if any):
- Open the files with conflicts.
- Edit and save the changes (choose what's correct).
- Stage the resolved files:
git add <file1> <file2> ...
- Continue the rebase:
git rebase --continue
- If you need to abort the entire rebase, you can do:
git rebase --abort
- Confirm your rebase result:
You should see your feature branch commits on top of the updated main commits.
git log --oneline
- Force push (if working on a remote shared feature branch):
Using
git push --force-with-lease origin feature
--force-with-lease
is safer than a raw--force
since it won’t overwrite remote work if another user also committed.
- Test the final state of your feature branch locally to ensure everything still builds and passes tests.
- Optionally create a backup or separate branch (e.g.
feature_backup
) to avoid losing progress in case of rebase mistakes.
- You run:
git checkout main git pull origin main git checkout feature git rebase main
- The rebase completes automatically (no conflicts), and you see a straightforward updated commit history.
- You verify the logs:
git log --oneline
- You then do:
git push --force-with-lease origin feature
- Everything works without manual intervention.
- You run:
git checkout main git pull origin main git checkout feature git rebase main
- Git reports a merge conflict in
src/app.ts
. - You open
src/app.ts
, fix the conflict markers (<<<<<<<
,=======
,>>>>>>>
), and save the file. - You stage changes:
git add src/app.ts
- Continue rebase:
git rebase --continue
- If further conflicts appear, repeat the process until done.
- Finally:
git push --force-with-lease origin feature
Rebasing your feature branch on top of the main branch is a straightforward process:
- Make sure
main
is current. - Switch to
feature
. - Execute the rebase.
- Resolve conflicts if needed.
- Push your updated branch.