Search code examples
gitgithubcommit

I have duplicated commits after pull request, How can I get rid of this?


We're developing a web application with my friends. Whenever we commit into this repo, GitHub always duplicate commits. I don't want this to happen and I don't even know this is true.

I want to explain how we do it:

  • Clone original repo with git clone original-repo.git
  • Create a new branch for a feature, update, fix etc. Let's call this branch a navbar
  • Commit these changes and publish a navbar branch
  • Create a pull request, admin merges this pull request into the main and deletes navbar branch

When I check commit history, there is duplicated commits and one of them has verified tag. What do we do wrong? Here is a screen shot for it:

image

For information, we used to fork the original repo and commit but that way we have to update original repo and I see a lot of update messages. That's why we're not doing that way any more.

Edıt: here is output of git log --oneline --graph img

Why there is 2 "update: updated navbar social and added href functionality" I commited only 1 time and merged it. What am I doing wrong? Is there a better way to contribute a project?


Solution

  • From your git log, it looks like the later commit is the merge commit that merges your pull request into main.

    You can see this because it has lines connecting it to both ecec914 and 29f6bdf.

    If you open a PR with only one commit, GitHub defaults to using the commit message as the PR description, and the PR description forms the commit message for the merge commit when you merge the PR.

    The later commit is verified because it is a commit made by GitHub, and they sign their commits with the GitHub commit key.

    GitHub will automatically use GPG to sign commits you make using the web interface. Commits signed by GitHub will have a verified status.

    source

    If you inspect the contents of the commits with git show -v <ref>, you should see that the first commit contains the changes and the second commit is empty (because its purpose is to be a merge commit that combines two histories that had diverged).

    If you want to avoid these merge commits in the future you need to change how you merge pull requests.

    One option is to merge to the main branch using git merge --ff-only <branch name> and then push. GitHub will automatically close the PR if it detects that you have merged it this way. This will only merge if you can fast-forward without creating a merge commit.

    If you want/need to do it from the GitHub UI you can use the drop-down on the Merge pull request button and select Rebase and merge which will rebase your commits on top of main and then fast-forward.

    Note that if you're signing your commits, Rebase and merge might remove your signature and replace it with GitHub's.

    enter image description here