متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 155Git · CI/CD

How to rebase a branch on main

Your feature branch is two days old and main has moved on. Rebase replays your commits on top of the newest main — the result looks as if you started today: a straight history, no merge commits, and the PR shows only your changes. This how-to rebases safely, handles conflicts, cleans up commits before review, and explains the one rule: never rebase a branch others work on.

  • 5 الدروس
  • 1 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

155.1

The quick answer

git fetch origin
git rebase origin/main
git push --force-with-lease

If a conflict stops the rebase: fix the file, git add FILE, git rebase --continue. To stop and go back: git rebase --abort.

الأوامر في هذا الدرس

155.2

Step by step

  1. Commit or stash your work first — rebase needs a clean working tree:
git status -s
git stash push -m "wip" 
  1. **Rebase onto the latest main** (quick answer). Git takes your commits off, moves the branch to origin/main, and applies them again one by one.
  2. Conflicts — one commit at a time:
git status
git add app/config.py
git rebase --continue

تذكّر

during a rebase --ours is main and --theirs is your commit.

  1. Clean up before review — squash "fix typo" commits into the real ones with an interactive rebase (in your own editor):
git rebase -i origin/main (editor)
pick 1a2b3c4 Add rate limit to login
fixup 5d6e7f8 fix typo
pick 9a8b7c6 Add tests for rate limit

Or mark fix commits as you make them and let Git sort them:

git commit --fixup 1a2b3c4
git rebase -i --autosquash origin/main
  1. Push the new history — the commits have new IDs, so a normal push is refused. --force-with-lease only overwrites if nobody else pushed to the branch meanwhile (safer than --force):
git push --force-with-lease
  1. The golden rule: rebase only your own branches. Never rebase main or a branch others have pulled — their history would no longer match.
  2. **Make git pull rebase by default** on feature branches:
git config --global pull.rebase true

الأوامر في هذا الدرس

155.3

Check that it worked

git log --oneline --graph -8
git log --oneline origin/main..HEAD

Your commits sit on top of the newest main (no merge commit), and the second command lists only your commits. CI on the PR is green again.

الأوامر في هذا الدرس

155.4

Common problems

ProblemCauseFix
cannot rebase: You have unstaged changesDirty working tree.Commit or git stash first.
The same conflict for every commitMany commits touch the same lines.Squash first, or merge main instead; rerere helps.
rejected … stale info on pushSomeone else pushed to your branch.git fetch, look at their commits, rebase again.
You lost commitsA wrong resolution or reset.git reflog shows the state before the rebase; git reset --hard HEAD@{N}.
Teammates have broken historyYou rebased a shared branch.Avoid it; they must reset to the new remote branch.
Not sure how to finishRebase still in progress.git status tells you; --continue or --abort.

الأوامر في هذا الدرس

155.5

Go further

1Why use --force-with-lease instead of --force after a rebase?

It refuses to overwrite the remote branch if someone else pushed to it meanwhile.

2Which branches should you never rebase?

Shared ones like main or branches other people already pulled.

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل