1

I have a repository to which several (3) authors contributed commits to the same branch of the repo. I want to merge it to the repository from which it was forked, but the admin of that repository asked me to squash the commits by author before they can be merged. I have managed to squash all commits (by all 3 authors) into a single one using

git reset --soft OLD_COMMIT_HASH

where OLD_COMMIT_HASH is the last commit before we started contributing.

How can I squash the commits while grouping them for each author? Is that possible at all? Even if it is possible, in the end, wouldn't the 3 squashed commits would be marked as authored by me, since I cannot commit on behalf of the other authors?

John
  • 465
  • 1
  • 6
  • 13
  • 1
    Did you try interactive rebase? – mkrieger1 Aug 14 '22 at 12:45
  • 2
    If the commits are made by the authors out-of-order (ex. A, B, C, A, C, B,...) and they changed different things on each commit, then squashing/grouping commits by author wouldn't make much sense. – Gino Mempin Aug 14 '22 at 12:45
  • 3
    If commits are interleaved, this is very likely not possible because you'll have conflicts if you change the order of commits. You should provide some exampe of you tree, because for now the question is quite fuzzy – OznOg Aug 14 '22 at 12:45
  • 1
    @torek You hammered this closed with your gold git badge, but that "dupe" is for when all commits by the same author are "in a row" (according to the title). What if the commits are like: Author 1, Author 2, Author 3, Author 1, Author 3, Author 2, and we want these 6 commits to be squared into 3 commits: Author 1 (x2), Author 2 (x2), Author 3 (x3)? – Nike Aug 18 '22 at 08:01

1 Answers1

2

I don't think there is a built in way to do this. If each of the 3 authors had their commits on separate branches, you could create a new branch at the same point before you started contributing and use git merge --squash branch_1 for each of the three branches. That way you have 3 commits for 3 authors.

If the commits are interleaved on the same branch, you will first have to separate them by git log --oneline --author=author_1 and cherry-pick them to a separate branch.

You can retain the author information via git merge --squash --author="author_1 <email_1>" branch_1

learning2code
  • 460
  • 4
  • 9
  • 1
    Thank you, however the commits from all 3 authors live on the same branch of the repository. Is there a way to untangle them and squash them by author in this case. – John Aug 17 '22 at 22:25
  • @John I already mentioned this in my answer. You will have to cherry-pick the commits by each author onto separate branches. The relevant command is `git cherry-pick ` – learning2code Aug 19 '22 at 12:23