How to delete all Git commits except the last five

后端 未结 2 602
醉梦人生
醉梦人生 2021-02-05 02:07

I have a very large Git repository which only contains binary files which are changed pretty often. Naturally the Git repository is much larger than the actual

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 02:52

    Ok, if you want what I think you want (see my comment), I think this should work:

    1. Create branch to save all the commits (and just in case):

      git branch fullhistory

    2. While still on master, reset --hard to the commit you want to retain history from:

      git reset --hard HEAD~5

    3. Now reset without --hard to the beginning of history, this should leave your workspace untouched, so it remains in HEAD~5 state.

      git reset --soft

    4. So now you have empty history on master, and all the changes you need in the workspace. Just commit them.

      git commit -m "all the old changes squashed"

    5. Now cherry-pick this 4 commits from fullhistory you want to have here:

      git cherry-pick A..B

    Where A is older than B, and remember A is NOT included. So it should be parent of the oldest commit you want to include.

提交回复
热议问题