简体   繁体   中英

Revert git merge without a merge commit

I branched a feature off develop. I made several commits on that feature branch. I then merged that feature branch back in develop & pushed everything to the origin.

I just noticed git did a fast forward which I didn't really want. Is it possible to revert that merge? In git log there is no commit for the merge (I wasn't asked for a comment either when I did it).

If it matters, nothing has changed since I pushed that merge.

If you still have the feature branch you can do the following:

WARNING: You should only do the following if you are 100% sure that nothing changed on develop since commits that you want to remove.

  1. Find the SHA of the last commit on develop before the merge. (ie the state of develop before the merge)

  2. execute the following while you are on the develop branch:

     git reset --hard <sha-of-old-develop> 
  3. execute

     git push -f 

Now the repository should be back to how it was before you merged.

A fast-forward does not have a merge commit by definition: the only operation that has been performed is the relocation of your develop branch. Thus, you can use the following commands to revert that update:

git checkout develop                       # Go back to develop
git reset --hard develop@{1}               # Reset develop to its previous location
git push --force-with-lease origin develop # Push it, discarding the fast-forward

Then, if you want to force a merge commit event if it's empty, use the --no-ff flag:

git checkout develop
git merge --no-ff feature/<x>

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