I'm trying the bread-and-butter usecase of git rebase
and it's not working as expected.
According to a guide, when there are new commits to your feature's base branch in remote: "You want to get the latest updates to the master
branch in your feature
branch, but you want to keep your branch's history clean so it appears as if you've been working off the latest master
branch."
Expectation is that after rebasing (git rebase master
), the commits in feature branch are re-written. But reality is local-feature branch ends up 1 behind and 2 ahead than remote-feature branch ([feature/123 ↓·1↑·2|✔]
). See bash-git-prompt for format.
I also tried git rebase --onto <SHA of master's head>
. Similar result [feature/123 ↓·1↑·1|✔]
.
So obviously both pull
and push
are rejected (see #4 below). What do I do next?
Notes:
- There are no conflicts/overlaps between changes in feature and master. So no conflict resolution etc.
- I know I can use
merge
. I want "clean history" (without merge commit), so want to userebase
. - I've read following and many more:
A working example:
1. Create first commit in master
git clone ssh://git@bitbucket.company.com/~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. Create a feature branch from master
and add a commit to 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. Add new changes to 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. Rebase feature
onto master
to bring latest changes from master in feature
✔ ~/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. Rebase feature
onto latest commit SHA from master
's git log
$ 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|✔]
$