Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Git commits on a branch

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
like image 564
Gingi Avatar asked Feb 27 '12 20:02

Gingi


People also ask

How do you remove all commits from a branch git?

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

Does deleting a branch delete commits?

What Happens If I Delete a Git Branch? When you delete a branch in Git, you don't delete the commits themselves. That's right: The commits are still there, and you might be able to recover them.


1 Answers

  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).

like image 127
Vi. Avatar answered Oct 13 '22 19:10

Vi.