Home > Mobile >  How to unmerge in GITHUB
How to unmerge in GITHUB

Time:07-16

I am having an issue with GitHub, which I believe to be easy to resolve but don’t know exactly how to find the solution, hope someone can give me some directions.

I have my project in different branches, where I use the main branch for production and different branches for different features of the project.

The issue is that I have merged a new branch to the main branch and for some error now the project is offline, I would like to restore the main branch to the previous version, but don’t know how to get it done, if any of you could give me a direction on how to get it solved I would strongly appreciate.

In the pictures below I can show exactly what I am saying, as you can see on the picture for the branchs my latest branch is the Winestore, when I merged this branch to the main branch my project got broken, so I would like to undo this merge and get main branch back to the merge I have done with the Testdeploy merge, which was working as expected.

if I simply try to merge the testdeploy branch it says that it is 19 commits behind, and therefore I can not do the merge.

enter image description here

enter image description here

Tks a lot Regards Victor

CodePudding user response:

Merging a branch creates a commit on the main branch (or multiple if you didn't squash the commits). Reverting the commit(s) created by merging the branches should fix your issue.

CodePudding user response:

If you've already reverted the merge locally, you can do git push --force origin master to forcibly overwrite the master branch with your local changes. However, you should only do this if you're the only person using this branch or if there are very few contributors. I have a more detailed response below if you're interested, as well as a safer alternative if you've got commits on top of the merge.

Long explanation

The git command-line tool is very powerful. But if I understand your question correctly, you're trying to undo a faulty merge. I'll try to explain along the way what a merge is and how they work.

If you're the only person working on this project, you can undo the merge locally and force push to master. However, I must warn you, don't do this if you share the repository with others or if you've made changes on top of the merge you want to undo. There is a safer way which I will detail later.

Using git reset (described above as well)

  1. On your local repository run git checkout master
  2. If you were already on master, stash your local changes with git stash
  3. Pull the latest changes with git pull
  4. Assuming the merge is the most recent commit (sounds like it is based on your description), you can reset the branch to before the merge using git reset --hard HEAD^. The HEAD^ simply means the commit before the most recent commit (i.e. its parent). In the case of a merge, this will undo it.
  5. Force push to master with git push --force
  6. Optional: if you stashed your local changes in step 2, add them back with git stash pop.

Using git revert

Git revert is safer, as it essentially undos a commit/merge by reversing the changes made by it. Another advantage is it is not destructive to the history. What I mean by this is each git commit's hash is unique and is generated based on the previous commit, and when we change a hash we can cause a mismatch in the history of changes among contributors. This can lead to merge problems and general confusion when people go to grab the latest changes.

  1. On your local repository run git checkout master
  2. Pull the latest changes with git pull
  3. Determine the hash of the merge you want to revert. You should be able to find it in the UI by looking at the history. Make a note of it.
  4. In the commit message for the merge, there should be two commit hashes beside Merge:. Keep these in mind as well.
  5. You can revert the merge with git revert --no-commit -m X sha1. sha1 is the commit hash of the merge. --no-commit tells git to wait before committing the changes. -m stand for mainline and it can have a value of 1 or 2. In order for git to undo a merge, you need to tell it which parent to revert with respect to. This is where those two commits in step 2 come into play. By specifying 1 or 2, you tell git which commit to revert with respect to.
  6. You can run git status or visually inspect the files to make sure your changes were reverted like you expect.
  7. Run git revert --continue. This might open a text editor like vim or vi. If those editors are opened, you can simply press and release Esc. Then type :wq and press enter. You should see a message that the revert was successful and a diffstat of the changes made.

Here's an example using some merges from the git project itself. Say I have the git history as follows:

commit dc8c8deaa6b5847733bd7df011a4c7b7d1a64e0a (HEAD -> maint, origin/maint)
Author:  <>
Date:   Tue Jun 7 18:48:52 2022 -0700

    Prepare for 2.36.2

commit d2b11e05e0293befe7d6ef58fb4bb0fb65ec468a
Merge: 67c305f722 6dfadc8981
Author:  <>
Date:   Wed Jun 8 14:27:53 2022 -0700

    Merge branch 'jc/clone-remote-name-leak-fix' into maint

commit 67c305f72290312e19113635023fed1e133d4f66
Merge: 363d54ff80 11f9e8de3d
Author: <>
Date:   Wed Jun 8 14:27:53 2022 -0700

    Merge branch 'ds/midx-normalize-pathname-before-comparison' into maint

Say I want to revert the merge d2b11e05e0293befe7d6ef58fb4bb0fb65ec468a. To do this, All merges really are is the combination of two branches. Therefore, they have two parents, the branch containing the changes I want to merge and the branch I'm merging into. I look at the parents (67c305f722 and 6dfadc8981). I see that 67c305f722 is the commit immediately before this merge in the maint branch. Running git revert -m1 d2b11e05e0293befe7d6ef58fb4bb0fb65ec468a will undo that merge. This leaves me with a new history of:

commit 00b87911b4052a1f998f73971624218079e74f42 (HEAD -> maint)
Author: <>
Date:   Sat Jul 16 01:35:27 2022 -0400

    Revert "Merge branch 'jc/clone-remote-name-leak-fix' into maint"

    This reverts commit d2b11e05e0293befe7d6ef58fb4bb0fb65ec468a, reversing
    changes made to 67c305f72290312e19113635023fed1e133d4f66.

commit dc8c8deaa6b5847733bd7df011a4c7b7d1a64e0a (origin/maint)
Author: <>
Date:   Tue Jun 7 18:48:52 2022 -0700

    Prepare for 2.36.2

commit d2b11e05e0293befe7d6ef58fb4bb0fb65ec468a
Merge: 67c305f722 6dfadc8981
Author: <>
Date:   Wed Jun 8 14:27:53 2022 -0700

    Merge branch 'jc/clone-remote-name-leak-fix' into maint

As you can see a new commit was created. This commit does reverse of the changes done by the original merge and creates a new commit to show those changes were reverted.

Also, a quick note on squashing. You can combine commits together into one single commit, and merges are no exception. I personally don't use it very often. I do squash commits on my local branch via rebases, but I like to have either a linear history or recorded merge commit when I combine my changes with production.

  • Related