How can I split up a Git commit buried in history?

前端 未结 6 858
刺人心
刺人心 2020-11-28 17:15

I flubbed up my history and want to do some changes to it. Problem is, I have a commit with two unrelated changes, and this commit is surrounded by some other changes in my

6条回答
  •  暖寄归人
    2020-11-28 17:56

    Manually correcting the history via cherry picking can also work for some cases.

    I perfer to use my git GUI (instead of the command line), my problematic commit was only 3 commits down, I haven't yet pushed any, and the following ones weren't exactly tidy either, so I opted for completely reconstructing all of them by cherry-picking, and it was faster than using the interactive rebase edits via the command line, but similar in approach.

    Here's how I did it in my favorite git GUI (I personally use SourceTree):

    1. Create a tag on the current state so that it isn't lost.
    2. Now move your actual local branch pointer to the messy commit.
    3. Reset (mixed) to the previous one, so that the files from the commit in (2) are kept.
    4. You can now split the commit in two or more by staging files that are needed and commiting with the correct message, until there's no unstaged files left.
    5. Cherry pick the next commit in line (from the history you've tagged). You do this by right clicking the desired commit and choosing "cherry pick". Goto (4), do until no more unaccounted commits left.
    6. Don't worry if as a result you have some commits that would be best squashed into one. You can squash them with an optional interactive rebase in GUI. It's as simple right-clicking the commit before the mess & clicking "Interactive rebase" then dragging commits onto each other to squash (fix the commit message to keep it simple), or move them up or down as desired.
    7. Remove the tag created in (1).

提交回复
热议问题