How do I perform a "merge commit" or "merge pull request" on "Github"?

Asked

Viewed 740 times

5

I want to contribute to a project on github only that I don’t want to post a PR with 28 commits. How do I (on Github) join these commits?

2 answers

6


There is no need to worry about Github, as the Project Owner can decide what to do with your PR. 3 possibilities will be presented:

  1. "Create a merge commit" (all commits enter the branch)
  2. "Squash and merge" (all commits on your branch will be combined into a single commit)
  3. "Rebase and merge" (rebase is done before merge)

Regardless of this feature above, you can also do the squash manually. The easiest thing would be to use a GUI if you’re not familiar with git commands.

If you want to do it via the command line, one possibility is to use the rebase, making

git rebase -i HEAD~[quantidade de commits]

or

git rebase -i [SHA do commit inicial].

If you’ve already pushed this branch, you’ll need to push the flag force, something like git push origin SuaBranch --force.

There are some other alternatives to achieve this same result, such as making a git reset and a new commit, for example.

4

There are different forms to do this. There’s no way to do this directly on Github, you’ll need to do it locally and then go up to Github for changes.

My favorite is to use the command rebase for this and marking the commits that will be joined with squash.

From the point you have 28 commits, the command would be:

git rebase -i HEAD~28

When you execute this command, the git will open a text editor to choose like this rebase will be done by displaying a message containing all messages from the 28 commits. Something like that:

pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file

Keep one of the commits with pick and the rest change to squash:

pick f7f3f6d changed my name a bit
squash 310154e updated README formatting and added blame
squash a5f4a0d added cat-file

Save the file and quit. Git will apply these changes and open the text editor again to choose the commit message:

# This is a combination of 3 commits.
# The first commit's message is:
changed my name a bit

# This is the 2nd commit message:

updated README formatting and added blame

# This is the 3rd commit message:

added cat-file

I particularly like to leave all messages (if not too many), to have the tracking of what was done with details. Another option is to comment on everything with # and create a single message, better formatted and complete, encompassing all the changes made.

After that, you can do the git push to your branch. If you’ve already pushed the 28 commits, you’ll need to use the option force to rewrite the history on the remote branch:

git push -f

And stay tuned for a possible problem: do not use this hint of rebase and commits union if someone has created a branch from this one.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.