简体   繁体   中英

Undo a git squash merge and git merge again

I have 2 remote branches feature and master. A squash merge was done onto feature branch from master. Later commits have happened both in master and feature branch. Now I tried to do another regular merge from master to feature. This turns out to be problematic since the new merge doesn't quite go along with the squash merge that already happened. A little search revealed that squash merge is the problem.

So now I am trying to do the following-

  1. Revert all the commits on feature branch till squash merge (I got an OK from team to revert the changes since there weren't many. I can do them later). For reverting I used

$ git revert --no-commit sha1

$ git revert --no-commit sha2

...

$ git commit -m "reverting the branch till the squash commit"

$ git push origin HEAD:refs/for/branch/feature

Now My feature branch shows an extra commit for the revert when I do git log.

  1. Now I want to try the following to merge the master to reverted feature -

    $ git merge --no-ff origin/master -m "merge from master"

    Gerrit review has been enabled on my remote repo

My question are -

  1. will this get in all the latest changes from master to feature including the master changes that old squash merge got in?

  2. Will this merge generate a single gerrit review ?

  3. Any simple solutions if my approach won't work ?

Will appreciate any help here. Thanks.

Answers to your question:

  1. I don't know, but, but I'd bet you a penny to a pound (or a cent to a dollar if you prefer) you will get into a nasty mess with git revert

  2. Don't know, but I doubt it - see below

  3. Yes (and hence this is not a totally useless answer I hope).

Do not git revert . git revert puts in another commit which does the opposite of the named commit, making your mess greater not less. git revert is a dreadful tool for undoing recent merge badness. It's a much better tool for undoing historical single commits. What you really want to do is to pretend the merge badness had never happened.

Instead:

  • git reset --hard [commitBeforeYouMadeTheMess]
  • git merge ... (or whatever you did before)

For safety, you could precede the lot by git branch backup-my-mess , or do it in a different branch git checkout -b see-if-I-can-clean-up-my-mess .

Note that if you have already pushed your mess (oh dear) you may need to do a force push to fix it.

If you want to avoid that, perhaps use a different feature branch ( foobar-2 or whatever), and branch that from before the problematic commit, then pretend the previous feature branch never existed.

This doesn't specifically address the gerrit point (because I'm not a gerrit person) but you need to get the git bit right first.

Is it possible to revert a commit via Gerrit as well. you can find the the button at the patchset page of the commit. It is very easy and convenient. And I suggest not to merge master into feature branch instead of rebase it.

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