0

I merged a feature branch into master. After that a colleague of mine merged some of his work.

After that he had to do a revert, which reverted all of my work. I guess that he did a git merge master into his feature branch, merged that branch and then reverted all of that.

I noticed that a week later. There have been more changes since then.

How can I get my changes back into master?

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • 2
    See stackoverflow.com/questions/5354682 https://stackoverflow.com/questions/1078146 https://stackoverflow.com/questions/8728093/… and [many more](https://stackoverflow.com/search?q=%5Bgit%5D+undo+revert) – IMSoP Dec 12 '22 at 15:36

1 Answers1

1

If the work has already been merged into main branch (or whatever the upstream is), then the easiest thing you can do is rewrite the work (which is a straight line, right?) so that git does not see it as merged anymore (well, because if you rewrite it, then it has not been merged before, right?).

So.... say the branch is feature and the upstream is main:

git checkout $( git merge-base feature commit-from-main-before-merge-of-feature )
# now we are at the commit where feature was started
git cherry-pick HEAD..feature
# now all commits that make up feature are on a new history
# that, to git, hasn't been merged
git branch -f feature # set feature branch over here

Now, if you try to merge feature into master, git won't complain.... well, it might complain because of the mess with the reversal but at least it won't tell you that what you want to merge is merged already. It will really try to merge.

eftshift0
  • 26,375
  • 3
  • 36
  • 60