0

Now there is a branch(branch1),
three people P1 P2 P3

they develop together on branch1
with the comimit log
P1 commit1
P2 commit2
P3 commit3
P3 commit4
P1 commit5
P2 commit6
...
P1 commit100

don't need to pay attention to the content of the submission records.
I just want to give an example of how they freely submit on this branch,
and there is a lot of commits

I want to create a new branch(branch2)

merge all code from branch1 into branch2 but each person will only have one commit,
such as

P1 commit1
P2 commit2
P3 commit3

May I ask if this is achievable? If possible, what should I do?

I don't know if these commands will work, such as
git cherry-pick
git rev-list
git filter-branch
git commit-tree
...
and complete interaction through scripts

1
  • You're looking for interactive rebase. This is probably a dup of this question.
    – TTT
    Commented Jun 14, 2023 at 8:03

1 Answer 1

0

If you are looking for a single commit on the branch per person, using short-lived feature branches will let each user squash the commits on their own branch before merging to the trunk.

In the case of your existing branch

  • If each person is modifying the same files, then the history should be preserved as it is. Re-ordering and squashing commits may cause problems.
  • If you know that each person is modifying completely separate parts of the codebase, then you might be able to try the following:
git rebase -i HEAD~6

When presented with the list of commits in the interactive editor:

pick 8aaaaaa CommitByPerson1
pick 7bbbbbb CommitByPerson2
pick 8cccccc CommitByPerson3
pick 7dddddd CommitByPerson1
pick 7eeeeee CommitByPerson2
pick 8ffffff CommitByPerson3

You can re-order them:

pick 8aaaaaa CommitByPerson1
pick 7dddddd CommitByPerson1
pick 7bbbbbb CommitByPerson2
pick 7eeeeee CommitByPerson2
pick 8cccccc CommitByPerson3
pick 8ffffff CommitByPerson3

And then squash them:

pick 8aaaaaa CommitByPerson1
squash 7dddddd CommitByPerson1
pick 7bbbbbb CommitByPerson2
squash 7eeeeee CommitByPerson2
pick 8cccccc CommitByPerson3
squash 8ffffff CommitByPerson3

You will be left with a single commit per-person.

2
  • I think your method is great. I want to write a script to automate it. Do you have any reference examples?
    – moe_sakura
    Commented Jun 15, 2023 at 9:06
  • I do not have any automation examples - I would recommend using short-lived feature branches to achieve one commit per person. The solution above is more of a 'fix' when things have become messy than a regular workflow.
    – braebdeb
    Commented Jun 16, 2023 at 10:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.