1

Refer to the following diagram:

my-branch      A---B---C---D
              /     \
master       E---F---G

During the merge from B to G I have inadvertently added files to master which I deleted from my-branch at D using git rm. Unfortunately, I have work at C which is not yet ready to be merged with master. Can I safely git rm the files in master and still merge my-branch (where the files have already been deleted) into master later?

In other words, by merging B with G, I added files to master at commit G which I do not want. These files are git rm'd from my-branch at D, but I do not want to merge D into master because C contains changes that are not ready yet. What is the best way to proceed?

4

2 回答 2

2

If the D commit only has the rm'ed files then I would move it ahead of the C commit, then merge that into master.

In this case that would be git rebase -i HEAD~2 then reorder the commits to put D before C. You can then merge D into master and end up with

A---B---D---C / \ E---F---G---H

If D has other changes, then you should redo the commit so it only has the rm'ed files, and then reorder them.

If you've already published my-branch then anyone who has pulled it will end up with a merge containing existing changes from C and D. In that case I would cherry-pick D onto master in order to get the files deleted prior to merging my-branch and then deal with a trivial merge conflict when you do merge into master. IMO it would be better for you to deal with the conflict once later, rather than everyone else deal with it now.

于 2017-01-26T19:14:53.933 回答
0

If you are unfamiliar or uncomfortable using interactive rebase, you could create a branch at commit G and cherry-pick D. Then pr or merge your branch into master.

于 2017-01-27T05:04:25.863 回答