Search code examples
gitmergegit-mergerebasegit-rebase

How to squash old commits coming from integrating another repo into a branch?


I am working in merging two repositories into one repository.

I integrated repo2 into a branch of repo1, keeping all the history of repo2. After that, I made extra commits to complete the code integration.

The problem is that now I have decided that I do not want to keep the history of repo2, as the number of commits is very high. Therefore, I tried to perform an interactive rebase. The problem here is that the history of repo2 contains commits with dates going back to a year ago.

The commits that have been added in the new branch look like this:

680a4b3  Integration          2 days ago
...
985d7ac  Integration          6 days ago    
23d3762  Latest repo2 commit  2020‑04‑20
...
23df1e4  First repo2 commit   2019‑05‑01

When I try to perform the interactive rebase going back to the first repo2 commit:

git rebase 23df1e4 ---interactive

The list of commits includes commits coming from both repo1 and repo2. Picking them manually would take too much time.

Is there any way to squash (or doing fixup, discarding the messages) all the repo2 commits automatically, but keeping the "Integration" ones?

Perhaps it is posisble to execute a rebase that only includes commits from the current branch, but I have not found how it can be done.

Thank you very much in advance.


Solution

  • Here is a way to squash the beginning of history :

    1. run rebase -i starting from the parent of "Latest repo2 commit" :

      git rebase -i 23d3762^
      
    2. in the actions, set the action of commit 23d3762 Latest repo2 commit to edit, save and close
      git will rewind to 23d3762, and wait for you to take action

    3. squash repo2's history :

      # use 'reset --soft' to go back to First commit,
      # while keeping the complete delta in the staging area :
      git reset --soft 23df1e4   # <- use id of "First commit" here
      git commit
      
    4. tell git to complete the interactive rebase

      git rebase --continue