How to avoid merge commits when syncing a fork
Tags: git, github, beginners, tutorial
Published: 2022-08-22 01:19:00

This article was originally posted on [Everything DevOps](https://everythingdevops.dev/how-to-avoid-merge-commits-when-syncing-a-fork/).

Whenever you work on open source projects, you usually maintain your copy (a [fork](https://docs.github.com/en/get-started/quickstart/fork-a-repo)) of the original codebase. To propose changes, you open up a [Pull Request (PR)](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests). After you create a PR, there are chances that during its review process, commits will be made to the original codebase, which will require you to sync your fork. 

To sync your fork with the original codebase, ideally, you would use the web UI provided by your Git hosting service or run a `git fetch` and `git merge` in your terminal, as indicated in this [Github tutorial](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork). But with a PR open, syncing your fork that way will introduce an unwanted merge commit to your PR. 

In this article, you will learn what merge commits are and how to avoid them with `git rebase` when syncing a fork with an original codebase. 

## What is a merge commit?

A merge commit is just like any other commit, it is the state of a repository at a point in time plus the history it evolved from. But there is one thing unique about a merge commit: it has at least two parent commits. 

When you create a merge commit, Git automatically merges the histories of two separate commits. This merge commit can cause conflicts and mess up a project’s Git history if present in a merged PR.

![](https://paper-attachments.dropbox.com/s_4038E27EBB83FC06153F31425FE835219A7D0419F41A63A1B27836B12EF94A13_1659630585735_annotely_image+42.png)

The annotated section in the image above shows a merge commit of two parent commits. Here is the [link to the merge commit](https://github.com/cilium/cilium/commit/fcc7837499b630df4e576396fd28d44007f77db1) to the image.

Now you know what a merge commit is. Next, you will learn how to avoid it when syncing your fork with an original codebase.  

## How to avoid merge commits when syncing a fork in Git.
**
To avoid merge commits, you need to [rebase](http://git-scm.com/book/en/Git-Branching-Rebasing) the changes from the original remote codebase in your local fork before pushing them to your remote fork by following the steps below.

**Step 1:**
Create a link with the original remote repository to track and get the changes from the codebase with the command below:


    $ git remote add upstream https://github.com/com/original/original.git

After running the above command, you will now have two remotes. One for your fork and one for the original codebase. If you run `$ git remote -v`, you will see the following:


    origin https://github.com/your_username/your_fork.git (fetch)
    origin https://github.com/your_username/your_fork.git (push)
    upstream https://github.com/original/original.git (fetch)
    upstream https://github.com/original/original.git (push)

In the above, `upstream` refers to the original repository from which you created the fork.

**Step 2:**
In this step, you fetch all the branches of the remote upstream with:


    $ git fetch upstream

**Step 3:**
Next, you rewrite your fork’s master with the upstream’s master using `git rebase`.


    $ git rebase upstream/master

**Step 4:**
Then finally, push the updates to your remote fork. You may need to force the push with “`--force`.” 


    $ git push origin master --force

**Note:** 
You can skip the `git fetch` step by using `git pull` which is `git fetch` + `git merge` but with the `--``rebase`  flag to override the `git merge`. The pull command will be:

****
    $ git pull --rebase upstream master
## Conclusion

In this article, you learned about merge commits and how you can avoid them when syncing your fork in Git using `git rebase`. There is a lot more to learn about `git rebase`. To learn more, check out the following resources:


- [Git rebase official documentation](https://git-scm.com/docs/git-rebase)
- [What is Git Rebase, and How Do You Use It?](https://www.simplilearn.com/what-is-git-rebase-command-article)
- [Difference Between git merge and rebase](https://www.baeldung.com/git-merge-vs-rebase)