简体   繁体   中英

Git rebase diverged

I want to rebase my branch (branch) on master (main). commit m1 means I have added a comment with the string m1 to the file. m2, b1 and so on are the same. What I do is:

git checkout branch
gitk --all

在此处输入图像描述

git rebase main

First, rewinding head to replay your work on top of it... Applying: b1 Using index info to reconstruct a base tree... M HelloWorld.java Falling back to patching base and 3-way merge... Auto-merging HelloWorld.java CONFLICT (content): Merge conflict in HelloWorld.java error: Failed to merge in the changes. Patch failed at 0001 b1 hint: Use 'git am --show-current-patch' to see the failed patch Resolve all conflicts manually, mark them as resolved with "git add/rm <conflicted_files>", then run "git rebase --continue". You can instead skip this commit: run "git rebase --skip". To abort and get back to the state before "git rebase", run "git rebase --abort".

#I fix the conflicts and add the file.
git rebase --continue

Applying: b1 Applying: b2 Using index info to reconstruct a base tree... M HelloWorld.java Falling back to patching base and 3-way merge... Auto-merging HelloWorld.java Applying: b3 Using index info to reconstruct a base tree... M HelloWorld.java Falling back to patching base and 3-way merge... Auto-merging HelloWorld.java Applying: b4 Using index info to reconstruct a base tree... M HelloWorld.java Falling back to patching base and 3-way merge... Auto-merging HelloWorld.java Applying: b5

git status

On branch branch Your branch and 'origin/branch' have diverged, and have 7 and 5 different commits each, respectively. (use "git pull" to merge the remote branch into yours) nothing to commit, working tree clean

gitk --all

在此处输入图像描述

I would like to know why it gets diverged and why the rebase do not work properly, what is causing it, and how can I fix it? am I doing the rebase wrong? When trying to figure out how to fix this issue, only thing I can find is how to go back to the first state (pic 1) with:

git fetch origin
git reset --hard origin/<branchname>

The rebase worked as intended. And if you hadn't pushed the branch before rebasing git status would be perfectly happy. The misunderstanding has more to do with remotes.

What happens when you push a normal branch. You have some new commits that have the remote branch in their history. When you push the commits get uploaded and the remote branch gets fast forwarded to where your local branch is.

The fast forward is important, because you can not solve merge conflicts on the remote. But if you rebase for example the remote branch will no longer be in the history of your branch. So you can not push normally and this is what git status is warning you about.

In your case you know why it diverged and have checked that nobody else has commits made to branch. you can solve this by git push --force-with-lease

I think your doing it wrong, you checkout to branch and rebase master, which mean you applying change to your branch

If you want to apply change feature branch to master:

git checkout master
git rebase <feature-branch>

ps: imho, would be nice to naming git branch not branch , but something like feature-x or any, to not confuse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM