Trimming Git Commits/Squashing Git History

前端 未结 4 707
滥情空心
滥情空心 2020-11-22 16:15

I check my code into a Git branch every few minutes or so, and the comments end up being things like \"Everything broken starting again\" and other absurdities.

Then

4条回答
  •  囚心锁ツ
    2020-11-22 16:47

    Using Soft Reset Instead of Rebase to Squash GIT History

    I think the length of VonC's answers speaks volumes -- literally -- about how complicated git rebase is. This is my extension of another answer to a question of mine.

    1. You have a branch ticket-201 that you branched from master. You want to pretend that all the commits from ticket-201 never happened, but that you did all the work in one shot.
    2. Soft reset to the branch point using git reset --soft hash where hash should be a commit hash that is in ticket-201's log.
    3. Commit your changes using add then commit. Now the branch history will only have the first commit and the new one with the new stuff.

    Making Up Histories From Arbitrary Commits in Different Branches

    Using resets you can rewrite the history as you see fit, though your edits will lose the charm of having the right timestamp. Assuming you don't care about that (the times/dates on your files will be enough, perhaps?), or if you want to fiddle with the commits as you go, you can follow these steps:

    1. Checkout a new branch at commit0 (pretend that's a hash): git checkout -b new-history commit0
    2. Now you can get the files from commit5: git reset --hard commit5
    3. Switch back to your index point: git reset --soft commit0
    4. Commit and this will be the second commit in the branch.

    This idea is simple, effective and flexible.

提交回复
热议问题