Search code examples
gitmergecommit

How to stop Git from making extra merge commits when team members make commits


I have a class of students in which we all work in a repository and constantly check in small status updates and code, etc.

We have noticed that in addition to our commits that we make, Git also seems to create merges and check these in as well, see screenshot.

These commits are full of changes from other team members and so are only confusing to us an clutter our git history.

What are these automatic commits and is there any way to configure Git not to make these commits?

enter image description here


Solution

  • Your comment explains why this is happening:

    We're simply updating files in each of our directories, pushing them as commits to the dev branch at GitHub and pulling commits from the dev branch from GitHub.

    It's the pull that causes the merge commits. Presumably you are all checking out the dev branch, making your commits, and at some point pulling the latest. When you pull commits from the remote version of your branch and you also have new commits on your local branch, the default setting in Git will create a merge commit.

    To avoid it, you have a few options:

    1. When pulling, use git pull --rebase. This will rebase your local commits onto the latest version of dev instead of creating a merge commit. If this is the way you decide to go, you can have everyone configure this to happen automatically.
    2. Don't pull anymore. If you want to update your local copy of dev, first do git fetch followed by git rebase origin/dev. The outcome is the same as #1, but this makes it more obvious that you are doing it.
    3. Similar to #2, you could start using separate branches instead of everyone working on dev. Before you merge into dev, do a git fetch followed by git rebase origin/dev.

    Number 3 is probably the best way to go, but is a slightly more complicated workflow. But it enables task switching if you want to work on different things at the same time.