简体   繁体   中英

How to rework a commit that has already been merged in an another branch

I have this situation

master    A--B--C
                 \
fix-15            D
                   \
other1        ...--(merged)--E

I missed something while working on D and I need to fix it on the same commit because I already sent a github pull request and the project owner wants a single commit per pull request. If D had not been merged in other1 I could reset and commit again and then push -f my changes. But because of other1 , git reset does not uncommit D .

If you want to change commit D, you'll have to make a replacement commit E, and probably a new pull request. If the project owner requires one-commit pull requests, you can amend your commit D, which will require you to replace other1 brand with a completely new one, which can create problems for people who have already pulled it, if they are any.

First of all, let me mark a few more commits on you graph:

master    A--B--C
                 \
fix-15            D
                   \
other1         X -- Y (merge) -- E

So, what you can do:

git branch other1_copy other1 # make a copy
git checkout other1
git reset --hard X # get to the state before merge
git checkout fix-15
# do your changes
git commit --amend # make commit D'
git checkout other1
git merge fix-15 # make a replacement merge commit Y'
git checkout other1_copy
git rebase --onto other1 Y # rebase your changes in 'other1' onto Y'
git checkout other1
git merge other1_copy # now you have 'other1' as E'
git branch -d other1_copy # delete the temporary branch

This is what it will look like at the end

master    A--B--C
                 \
fix-15            D'
                   \
other1         X -- Y' (merge) -- E'

Even though this does what you asked for, it is a bit complex and it's quite possible they are ways of solving your problem without messing with your branches like this. One of them is adding another commit to fix-15 , and merging it to other1 via normal merge:

master    A--B--C
                 \
fix-15            D ------ F
                   \        \
other1         X -- Y -- E -- G

Then, if you have a branch tracking your upstream, you can create a one-commit pull request like this:

git fetch upstream # let's call your upstream remote `upstream`
git checkout -b pull-branch upstream/master
    # ^ you'll be using this to create a pull request
git merge --squash other1 && git commit
    # ^ creates a squashed commit will all the changes from `other1`
# now create a pull request from this branch

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