Вопрос

How do I remove commits that are branched off master?

I have added a large library, along with a very rich commit history (and that predates my code) to a subdirectory via Git subtree. I'd like to retroactively squash that entire history, but still be able to merge in new commits to the library.

I have tried various combinations of git rebase but I never get the expected result [*].

My repository looks something like:

A---B-----------F---G master
               /
  ... C---D---E

and I'd like for it to look something like:

A---B-----------F'--G' master
               /
              E'

or:

A---B-------E'--F'--G' master

[*]:

  • git rebase --onto C E master
  • git checkout F; git rebase --onto C E master
Это было полезно?

Решение

  1. This is history editing. You will end up in something like

    A---B-----------F'---G' master
                   /
                  E'
    
  2. Merging will be a problem after this, because of Git will not be able to find common parents between your history and libraries's history.

  3. To actually do it you need to

    1. Reset to B (creating tag or branch for G to keep it around)
    2. Perform merge with --no-commit.
    3. Rebase or cherry-pick G here (it will be G')

    The history will look like

    A---B-----------F'---G'
    

To make the shallow clone of the library, you need to do something like this (Warning: untested):

  1. Save commits from F (not including) to G (including) to a patch (git format-patch F --stdout > ~/saved_commits.patch)
  2. Reset to B. Ensure there are no branches that are poiting to F, E or G
  3. Remove the remote together with it's ref namespace git remote rm
  4. Erase reflogs: git reflog expire --expire=now --all
  5. Actually remove things from git: git gc --prune=now. Now you should see the repository shrank.
  6. Re-add the remote for library.
  7. git fetch --depth=10 libraryremote
  8. Repeat the merge (the usual way)
  9. Apply saved commits (git am ~/saved_commits.patch).

To migrate into submodules solution (the best option probably), you need to rollback to the state before merge and set up submodules, then substitute each merge with changed commit-id for submodule. Unlike for the case of splitting out project directory to submodule I don't know the automated solution for this (but it can be implemented the similar way).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top