最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

git - Rebasing branch from later branch back onto master directly - Stack Overflow

programmeradmin2浏览0评论

After updating my local master, I routinely rebase my local branches.

However, this time, I fot to checkout master back between rebases and rebased one local non-master branch (branch_2 in my example) on top of the other (branch_1). I use IDEA's "Checkout and rebase onto..." feature, it's a convenient shortcut.

How do I now "re-rebase" the mis-rebased branch (branch_2) onto master? Technically, it's already on master (even though there's another branch in between), so rebasing onto master won't work.

* branch_2
*
* branch_1
*
* master
*

After updating my local master, I routinely rebase my local branches.

However, this time, I fot to checkout master back between rebases and rebased one local non-master branch (branch_2 in my example) on top of the other (branch_1). I use IDEA's "Checkout and rebase onto..." feature, it's a convenient shortcut.

How do I now "re-rebase" the mis-rebased branch (branch_2) onto master? Technically, it's already on master (even though there's another branch in between), so rebasing onto master won't work.

* branch_2
*
* branch_1
*
* master
*
Share Improve this question edited Mar 19 at 8:50 mkrieger1 23.5k7 gold badges64 silver badges82 bronze badges asked Mar 19 at 7:30 Sergey ZolotarevSergey Zolotarev 1,8691 gold badge11 silver badges32 bronze badges 1
  • However, this time, I fot to checkout master back between rebases This is also a very, very strong argument for never running git rebase with less than two arguments because then which branch is current does not matter. – hlovdal Commented Mar 24 at 20:39
Add a comment  | 

3 Answers 3

Reset to default 7

Simply use

git rebase --onto master branch_1 branch_2

i.e., recreate on master all commits from branch_1 to branch_2 – or more precisely, all commits which are contained in branch_2, excluding those which are contained in branch_1 – and then update the branch_2 pointer.

An alternative interpretation is: change the base of branch_2 from branch_1 to master.

IMO the most comprehensive explanations for how --onto works are given in What is the difference between `git rebase master` and `git rebase --onto master`?

The key is to reset branch_2 to the state it was in before you mistakenly rebased it onto branch_1.

Original Commit:

You need to find the commit hash of the last commit on branch_2 before the incorrect rebase. I think the reflog command is your best friend here. It records every change to your branch's HEAD.

In your terminal, run: git reflog branch_2
Look for the entry that represents the state of branch_2 before the rebase.
Copy the commit hash from that entry.

Reset branch_2:

Use the git reset command to move branch_2's HEAD back to the original commit.

In your terminal, run: git reset --hard <original_commit_hash>: this will rewind branch_2 to its state before the rebase.

Rebase onto master:

Now that branch_2 is back to its original state, you can rebase it onto master correctly.

In your terminal, run: git rebase master

Here is an example of what I mean: pls do not copy paste, this: it's just an example

# Example reflog output (find the correct commit hash)
# git reflog branch_2
# 6a4b7c8 HEAD@{1}: rebase (finish): returning to refs/heads/branch_2
# 9d3e5f2 HEAD@{2}: rebase (start): checkout branch_1
# 1234567 HEAD@{3}: checkout: moving from master to branch_2  <-- Original state

# Reset branch_2
git reset --hard 1234567

# Rebase branch_2 onto master
git rebase master

rebase -i master will let you choose and rearrange the changes as you see fit -- it brings up an editor and you simply delete the changes you don't want and keep the rest (or reorder them if you want that, or squash 2 or more together).

发布评论

评论列表(0)

  1. 暂无评论