Search code examples
gitversion-controlgit-mergegit-pullgit-fetch

Git: How to pull from another repository without adding a merge commit


This is a project where multiple developers are working. Assume that all work is done on the master branch, and that there is a remote repo that has the following history today:

A --- B --- C

Today I went ahead and cloned this repository into one of my sandboxes (let's call this work_sb01). I did some work, and have committed my changes locally (there could be just one commit or multiple commits). The Git history for this sandbox looks as shown below:

A --- B --- C --- X

Two days from today, I clone the same repository into another sandbox (let's call it work_sb02). As expected, the repo HEAD has moved, so the history might now look something like this:

A --- B --- C --- D --- E

I need to be able to pull the changes that I made in work_sb01 and bring them into work_sb02. I'm currently doing this with the following command:

cd <path_to_work_sb02>
git pull <path_to_work_sb01> master

This gets the job done, but it creates a merge commit. I have now a requirement to avoid adding merge commits into the project Git log. How can I pull the changes from work_sb01 and make them the most recent commit in the history? In other words, the local history for work_sb02 should look like the one shown below:

A --- B --- C --- D --- E --- X

Solution

  • Here is the right way to do this from work_sb02:

    cd <path_to_work_sb02>
    git remote add sb01 <path_to_work_sb01> 
    git fetch sb01 
    git checkout master 
    git rebase --onto master master sb01/master