如何在远程基础分支中的新提交之上重新调整本地提交

问题描述 投票:0回答:1

我正在尝试

git rebase
的主要用例,但它没有按预期工作。

根据指南,当远程中的功能基本分支有新提交时:“您希望在

master
分支中获取
feature
分支的最新更新,但希望保留分支的历史记录干净,所以看起来好像您一直在处理最新的
master
分支。”

期望在变基(

git rebase master
)之后,功能分支中的提交被重写。但实际情况是本地功能分支比远程功能分支落后 1、领先 2 (
[feature/123 ↓·1↑·2|✔]
)。 请参阅 bash-git-prompt 了解格式。

我也尝试过

git rebase --onto <SHA of master's head>
。类似的结果
[feature/123 ↓·1↑·1|✔]

我做错了什么?

备注:


一个工作示例:

1.在
master

创建第一个提交
git clone ssh://[email protected]/~kash/rebase-test.git
cd rebase-test/
date > only-changed-in-master.txt
git add only-changed-in-master.txt 
git commit -m 'creation'
git push

2.从
master
创建一个功能分支,并将提交添加到
feature

git checkout -b feature/123
date > only-changed-in-feature.txt
git add only-changed-in-feature.txt 
git commit -m 'creation'
git push --set-upstream origin feature/123

3.添加新的更改
master

git checkout master
date > new-file-in-master.txt
git add new-file-in-master.txt 
git commit -m 'adding new file in master'
git push

4.将
feature
重新设置为
master
以带来 master 功能的最新更改

✔ ~/workspaces/rebase-test [feature/123|✔]
$ git rebase master
Successfully rebased and updated refs/heads/feature/123.
✔ ~/workspaces/rebase-test [feature/123 ↓·1↑·2|✔]
$ git push
To ssh://bitbucket.company.com/~kash/rebase-test.git
 ! [rejected]        feature/123 -> feature/123 (non-fast-forward)
error: failed to push some refs to 'ssh://bitbucket.company.com/~kash/rebase-test.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
✘-1 ~/workspaces/rebase-test [feature/123 ↓·1↑·2|✔]
$ git pull
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge (the default strategy)
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
✘-128 ~/workspaces/rebase-test [feature/123 ↓·1↑·2|✔]
$ 

5.将
feature
重新设置为来自
master
git log

的最新提交 SHA
$ git checkout feature/123
$ git reset --hard origin/feature/123
HEAD is now at c3a8f2c creation
✔ ~/workspaces/rebase-test [feature/123|✔]
git checkout master
git log 
git checkout feature/123
...
$ git rebase --onto 5e3a2ed1f3657e7cde741d014a2c86afd99f1d92
Successfully rebased and updated refs/heads/feature/123.
✔ ~/workspaces/rebase-test [feature/123 ↓·1↑·1|✔]
$ 
git git-merge git-rebase
1个回答
0
投票

你没有做错任何事。跟踪信息正确表明您与跟踪分支的关系

origin/feature/123
而不是
origin/master

因为你所做的事情,你在上游分支上一落后一领先

© www.soinside.com 2019 - 2024. All rights reserved.