I have certain merge commits on my master branch whenever I updated my repo from central repository. Now whenever I make a new branch from master these commits appear in pull requests which are very annoying. How can I remove these commits and avoid them in future while updating? I know that these kinds of questions have been asked but nothing has worked for me yet.
How to Undo a Merge Commit in Git. You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog .
In case you are using the Tower Git client, undoing a merge is really simple: just press CMD+Z afterwards and Tower will undo the merge for you!
To undo a git merge, you need to find the commit ID of your last commit. Then, you need to use the git reset command to reset your repository to its state in that commit. There is no “git revert merge” command.
After the merge request has been merged, use the Revert button to revert the changes introduced by that merge request. After you click that button, a modal appears where you can choose to revert the changes directly into the selected branch or you can opt to create a new merge request with the revert changes.
The problem here is that your master branch contains commits (specifically, merge commits) which aren't part of the upstream repository. Because of this, any branches you base off of this master branch will also have those commits, so any PR asking to merge one of those branches to the upstream master will also include them.
To fix this, you can rebase your local master onto the master from the upstream repository. By default, git rebase
ignores merge commits, so this should eliminate any superfluous commits from the branch's history. Assuming you have the upstream repository set up as a remote named upstream
, you can do that like this:
git fetch upstream
git checkout master
git rebase upstream/master
git push -f origin master
Or alternately, if you're sure your master branch contains no changes that you want to keep which aren't already in the upstream repository's master branch (which, based on what you've stated in the question, should be the case), you can simply reset it to match the upstream master:
git fetch upstream
git checkout master
git reset --hard upstream/master
git push -f origin master
After this is done, rebase your feature branches onto the new master branch:
git checkout feature
git rebase master
git push -f origin feature
This should remove the extra merge commits from your pull requests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With