简体   繁体   中英

Git rebase with conflicting branches

I currently have the following situation in my git repository

A - B - C - D - E - F (master)
     \- D' - E' - G (branch)

I branched out from D at some point, but it seems that someone went back and introduced a C commit and then I was completely out of sync. Now, I want to rebase my changes in G(branch) to F(master) . That is, I would like to have the following instead

A - B - C - D - E - F(master)
                     \- G(branch)

In other words, completely discarding D' , and E' . What is the way to do this? I tried git rebase but I was getting many conflicts that kept asking me to merge changes in D ' and E '.

To rebase onto master , carrying only one commit (that being G ):

git rebase --onto master HEAD~1

The HEAD~1 specifies the "upstream", overriding whatever default upstream is used. git rebase will replay only commits in the current branch that are not in upstream. So that isolates just the one commit. --onto specifies where to hard reset to before cherry picking the commits.

Speaking of cherry-picking, we could also just do the equivalent steps manually:

# drop all work, and just become equivalent to master
git reset --hard master

# pick just G out of the dropped work, bringing it into master
git cherry-pick <sha-of-G>

A third way would be to do an interactive rebase:

git rebase -i

then edit out the unwanted commits, keeping only G. If you don't know interactive rebase, this approach introduces a useful skill, so is arguably the best of the three.

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