1

I am working on a React project that uses gh-pages to deploy a website. I accidentally merged my master branch with my gh-pages branch and brought all of the changes that were on master to gh-pages. In total, it added 24 commits to my gh-pages branch that weren't there before.

I was wondering if there was a way to revert these changes and to take my gh-pages branch back to the 6 commits that were there to begin with?

If that is possible, is there a way to remove those 24 extra commits on the gh-pages branch from GitHub?

I have looked at resources related to git reset --hard and git rebase and I am unsure if they would solve my problem. I don't want to destroy the git structure of this project more than it already is.

arctic
  • 609
  • 3
  • 11
  • 19

1 Answers1

0

The safest thing to do is to git revert the merge commit you made by accident. By doing a git revert it should be noted that you are adding a new commit which functionally undoes the merge commit from master which you did by accident. Here is how to do this:

# from gh-pages
git log

# find the SHA-a of the merge commit, and then do
git revert -m 1 <merge commit hash>

Note that you probably want -m 1 here, which tells Git to revert back to the first parent, which typically would be your gh-pages feature branch.

Note that doing a revert commit here is preferable to trying to cut out the merge commit. The reason for this is that actually removing the merge commit means rewriting history, which can cause trouble for anyone else using your gh-pages branch.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • In pushing these changes to GitHub, would it also remove the erroneous commits there as well? – arctic Aug 10 '20 at 07:57
  • @arctic Doing `git revert` doesn't actually _remove_ any commits at all. Instead, it makes a _new_ commit, on top of your branch, which more or less undoes whatever changes were made by the merge commit. To your exact question: yes, once you push to GitHub, the branch will behave as if you never made the erroneous merge commit with `master`. – Tim Biegeleisen Aug 10 '20 at 07:59
  • While reverting a faulty merge is the safest thing to do, there are other factors to take into consideration. Is `master` going to be merged into `gh-pages` at a later point in time? If yes, those reverted commits won't be coming back with a new merge. – Enrico Campidoglio Aug 10 '20 at 08:23
  • @EnricoCampidoglio Good point, and if the OP has not yet pushed, then maybe a rebase might make more sense. – Tim Biegeleisen Aug 10 '20 at 08:25