Merge after Cherry-pick with Git results in multiple same Commit histories

Asked 1 years ago, Updated 1 years ago, 353 views

Merge after Cherry-pick on Git resulted in multiple identical commits.

When I did the following, I made a commitment history with the same contents.
How do I resolve this?

[Prerequisite]
Branch:A, Branch:B
[Procedure]
1.Cherry-pick Commit 1 of Branch:A to Branch:B
2.Cherry-pick Commit 2 of Branch:A to Branch:B
3. Merge Branch:A to Branch:B

Example:
1.git checkout branchB
2. git cherry-pick commitid1 (sha1 in Branch: A)
3. git cherry-pick commitid2 (sha1 in Branch: A)
4.git merge origin/branchB

After the example 4 was finished, several commitments corresponding to commitid1, commitid2 were created (commitid is not the same).
What should I do to prevent the same commitment from being recorded in the history?
Also, how can I summarize or delete the duplicate history that has already been pushed?
I would appreciate it if you could let me know.

git github

2022-12-13 06:11

1 Answers

When you commit as follows,

 git init
touch hello.txt
git add.
git commit-minit
git branch branchA
git branch branchB
git checkout branchA
echo'edit1'>>hello.txt
git commit-am 'commit1'
echo'edit2'>>hello.txt
git commit-am 'commit 2'
git checkout branchB
git cherry-pick-x branchA^^..branchA
git merge -- no-edit branchA

The git log command results in the following:

commit441d9cdca924b7979a0ea95e70b3a6d9df43a012 (HEAD->branchB)
Merge: f10473eac17f85
Author: yukihane<[email protected]>
Date—Tue Dec 1309:35:41 2022+0900

    Merge branch 'branchA' into branchB

commit f10473ee50e5d6f1 a73d5e5be95b9cadfafb00d1
Author: yukihane<[email protected]>
Date:Tue Dec 1309:35:40 2022+0900

    commit2

    (cherry picked from commit ac17f85eda899e20952ad5d4ef415af4174cb1)

commit ac17f85eda899e20952ad5d4ef415af4174cb1 (branchA)
Author: yukihane<[email protected]>
Date:Tue Dec 1309:35:40 2022+0900

    commit2

commit bd740a9896832c5a6218921fe90c3ff93d3ae438
Author: yukihane<[email protected]>
Date:Tue Dec 1309:35:40 2022+0900

    commit1

    (cherry picked from commit d0b43f3e15de1177e1c21da4f92231eb065a978e)

commit d0b43f3e15de1177e1c21da4f92231eb065a978e
Author: yukihane<[email protected]>
Date:Tue Dec 1309:35:40 2022+0900

    commit1

commit d7b431119cb57e33d986dcbd0c9c1c34653a2a2e(main)
Author: yukihane<[email protected]>
Date:Tue Dec 1309:35:39 2022+0900

    init

It is natural that it should be recorded in the history because it is committed.If you want to delete it from the history, you must delete the commit in rebase.
However, you can hide it at the output.

--first-parent option is available if you want to hide the commitment of the branches merged with gitmerge (in this case branchA):

$git checkout branchB
$ git log -- first-parent
commit441d9cdca924b7979a0ea95e70b3a6d9df43a012 (HEAD->branchB)
Merge: f10473eac17f85
Author: yukihane<[email protected]>
Date—Tue Dec 1309:35:41 2022+0900

    Merge branch 'branchA' into branchB

commit f10473ee50e5d6f1 a73d5e5be95b9cadfafb00d1
Author: yukihane<[email protected]>
Date:Tue Dec 1309:35:40 2022+0900

    commit2

    (cherry picked from commit ac17f85eda899e20952ad5d4ef415af4174cb1)

commit bd740a9896832c5a6218921fe90c3ff93d3ae438
Author: yukihane<[email protected]>
Date:Tue Dec 1309:35:40 2022+0900

    commit1

    (cherry picked from commit d0b43f3e15de1177e1c21da4f92231eb065a978e)

commit d7b431119cb57e33d986dcbd0c9c1c34653a2a2e(main)
Author: yukihane<[email protected]>
Date:Tue Dec 1309:35:39 2022+0900

    init


2022-12-13 07:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.