M
M
Mikhail Emelyanov2021-08-12 14:33:57
git
Mikhail Emelyanov, 2021-08-12 14:33:57

How to merge two non-consecutive commits?

Suppose there are three commits:
commit 1 - minor edits;
commit 2 - major rework;
commit 3 - minor edits again.

I want to merge commits 1 and 3 into one using rebase. I don’t understand how to do this, because with the help of squash you can only glue consecutive commits, that is, you can, for example, glue 3 to 2, but not 3 to 1.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2021-08-12
@sepulkary

It may not be easy. Evaluate the need for this operation before proceeding, you can greatly damage the repository / code.
The task can be completed via an interactive rebase. Conflicts are likely to occur during the process. I would do it in 2 steps. First of all, I would arrange the commits in the right order.
git rebase -i хеш_коммита_3^
In the editor that opens, you can put the commits in the right order.
After saving the file, you will most likely get a message about unresolved conflicts - you need to resolve them all, add them to the index, and continue rebase through git rebase --continue
After success, you can repeat the interactive rebase to do a squash.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question