跳到正文
DEV Community· Himanshu Kumar·· 2 小時前AI 評分53

Git 父分支與遠端同步,子分支仍可能保留舊提交

When an up-to-date Git branch leaves its child behind

AI 導讀

Git 父分支即使與遠端完全一致,子分支仍可能指向父分支被 amend 前的舊提交,令 GitLab 合併請求差異包含屬於父分支的變更。文中以本機 bare remote 重現問題,指出 git status 沒有 ahead 或 behind 計數,並不代表兩個分支的歷史一致。

正文

Himanshu Kumar

A parent branch can match its remote exactly while its child still contains an earlier version of that parent. In a stack of GitLab merge requests, that can make the child diff show changes that belong to the parent. This showed up in a local test for GitLab CLI issue #8283. The useful clue was the commit graph hiding behind a perfectly reasonable git status.

Reproduce it without a GitLab account

This creates a disposable repository and a local bare remote. The lease-protected push below goes to that local directory.

demo=$(mktemp -d)
git init -q --bare "$demo/origin.git"
git init -q -b main "$demo/work"
cd "$demo/work"
git remote add origin ../origin.git
git config user.name "Stack Demo"
git config user.email stack-demo@example.invalid
printf "mode=base\n" > settings.txt
git add settings.txt
git commit -qm "Initial setting"

git switch -qc parent
printf "mode=original\n" > settings.txt
git commit -qam "Set the default mode"
old_parent=$(git rev-parse HEAD)

git switch -qc child
printf "consume mode\n" > app.txt
git add app.txt
git commit -qm "Read the default mode"
git push -qu origin main parent child

git switch -q parent
printf "mode=reviewed\n" > settings.txt
git commit -qa --amend --no-edit
git push -q --force-with-lease origin parent

The amend changes the parent commit ID while preserving its subject. The child still points to the old commit. Moving origin/parent along with parent makes Git report that the parent is up to date without changing the child at all.

Ask Git about the parent and child

git status --short --branch
git merge-base --is-ancestor parent child
git log --format=%s parent..child

The status line is ## parent...origin/parent, with no ahead or behind count. The ancestry check exits with status 1. The log lists Read the default mode and Set the default mode. That second entry is the old parent commit, even though the new parent has the same subject. This is the difference between a branch agreeing with its upstream and two branches agreeing on their history.

Replay the child from the boundary you know

In this disposable repo, old_parent is the exact boundary captured when the child was created. First check that it is an ancestor of the child. Then keep a reference to the pre-rebase child and replay only the commits after that boundary.

git merge-base --is-ancestor "$old_parent" child &&
  git branch backup/child-before-restack child &&
  git rebase --onto parent "$old_parent" child

git merge-base --is-ancestor parent child
git log --format=%s parent..child
git show child:settings.txt
git diff --stat parent..child

Now the ancestry check succeeds. The log lists only Read the default mode, settings.txt says mode=reviewed, and the diff contains only app.txt. There is no publication step in this second block. On a shared branch, fetch and inspect the child’s remote tip before rewriting history, especially if a teammate has pushed since you last looked.

The CLI regression behind this example checks three outcomes with a local bare remote: a local-only sync keeps the remote child unchanged, a later regular sync publishes the rebased child, and another sync keeps the same commit IDs. That tests the result users care about: each merge request should still show its own layer after the parent changes.

Reference: Git’s merge-base documentation.

AI disclosure: This article was drafted by an AI agent. The Git example and CLI regression were run in disposable local repositories.

來源:DEV Community · dev.to