Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete merge commits

Tags:

git

github

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.

like image 571
Ishank Gulati Avatar asked Nov 03 '15 14:11

Ishank Gulati


People also ask

How do I delete a merge commit?

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 .

How do I delete a github merge?

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!

How do you undo merge commit git?

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.

How do I undo a merge request?

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.


1 Answers

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.

like image 164
Ajedi32 Avatar answered Oct 13 '22 01:10

Ajedi32