Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid unwanted merge commits and other commits when doing pull request on GitHub

Tags:

git

github

I forked a project on Github.

Let the remote upstream be upstream and my remote repository be origin. My local master branch is set to track the remote master branch. Then I added some stuff in local master, and I merged with the upstream every now and then.

Not until today when I want to issue a pull request did I find the problem: the pull request consists those merge commits, and those unwanted commits that I did previously without care. However what I want is just to submit the last commit I did, which should be pulled as a single commit. What can I do to rescue this?

like image 287
Ivan Xiao Avatar asked May 11 '11 18:05

Ivan Xiao


People also ask

How do you restrict who can merge pull request GitHub?

No, GitHub doesn't let you restrict who can perform a merge. However, if you want to require a specific group of people to approve a PR before merging, use the CODEOWNERS file and require an approval from a code owner before merging in the branch protection settings.

Why is GitHub showing all my previous commits in every pull request?

This happens with GitHub when you squash commits merged in from the target branch. I had been using squash and merge with Github as the default merge strategy, including merges from the target branch.


2 Answers

Instead of merging you want to rebase. You can do this manually, or automatically when pulling.

git pull --rebase upstream master
git push --force origin master

Once you've started doing merges though this will get hard to do, you'll need to reset the branch back to before you did a merge commit.

like image 105
Arrowmaster Avatar answered Nov 11 '22 15:11

Arrowmaster


If I understand your question, you want to get rid of the intermediate/throwaway commits that you did in your branch. Try something like this:

git checkout -b for-upstream remotes/origin/master (create a new branch from the upstream origin)
git cherry-pick <sha-of-the-one-commit-you-want-to-submit> (fix any conflicts if necessary)

this should give you a local "for-upstream" branch which contains just the upstream master + your 1 commit. You can then submit that branch for pull request

like image 41
csd Avatar answered Nov 11 '22 15:11

csd