Merge multiple old commits in git which have alrea

2019-08-14 05:29发布

I have a bunch of old commits which I have already pushed. These are small changes 'e.g. typos', and I want to merge them together so that I don't have 100s of similar commits.

e.g. this:

7af8cee5715e266bf249891cf66f832cf8bb6606 typo 53104d19d1e2eba92baf36b8384100c461e417c1 typo 9e5afd2afc5d6051f568ce3a441eebf087c9ea46 typo fb8be7c9c54ae2d9ee3ed15971de49104729e4d6 ok 48828aaf959ee76d77a74dc35b8455542d0dbb8b fixed links 6387e73bd692024acbb67c1a843348dd6bd01bb8 fixed link baff3fc602faab37fbd0bf7df9c61ff367121985 fixed htaccess issue 2b3e66c19af49030a1da16f9fb7955c4d0f9aa3e fixed htaccess issue 5224690d2a44ec0c4872bedb3b54fb55af75530a fixed iplogging issue

Note: These are not directly behind the last commit, these are from months ago.

标签: git
1条回答
Root(大扎)
2楼-- · 2019-08-14 05:50

Your only option is to perform an interactive rebase on the commits and force-push the rewritten commits to the server-side repository. I recommend you keep the history as-is if the commits since then contain merge commits.

  1. First, find the SHA of the first commit you want to change, e.g. 522469.
  2. Check if other branches have been merged since: git log --oneline --merges 522469~1. If this generates output, do not run the interactive rebase.
  3. Start the interactive rebase: git rebase --interactive 522469~1
  4. git will present you the list of commits starting at 522469 that you may reorder. Change the pick command (first column) to either squash or fixup for commits you want to meld.
  5. Reorder and squash/fixup to your heart's content, save the file and close the editor.
  6. After the interactive rebase is done, check your build.
  7. As the last step, force-push rewritten history to the remote repository: git push --force origin HEAD

Some things to keep in mind:

  • If there are merge commits, the interactive rebase will remove them. So please don't git rebase --interactive if #2 generated output.
  • The interactive rebase only affects the current branches' commits (i.e. reachable by HEAD). Any branches based off of any commits in the editor window (git branch --contains 522469) will need to be rebased off their rewritten counterparts. So there might be more work involved than displayed above. Check git rebase --onto.
  • In the future, when you find that you need to change/fix the most recent commit, use git commit --amend to rewrite the HEAD commit instead of creating a new commit. Another option is to git rebase --interactive with less commits (e.g. not from months ago but say 2 hours ago, HEAD~10) which might present less potential for conflicts.
查看更多
登录 后发表回答