R
R
Romi2021-05-03 10:52:20
git
Romi, 2021-05-03 10:52:20

If I made several commits to the wrong branch in a hurry, is it possible to somehow selectively transfer them to another?

Well, for example, I forgot to switch, and made a couple of commits to the master.

Or, in general, in branch-1, but it was necessary in branch-2.

How can I resolve all this?

Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuriy Vorobyov, 2021-05-03
@romicohen

Depends on what kind of migration you want to do and what state of the branches are now
. Suppose you have this situation:

branch-1 A - B - C - D - E
branch-2 F

Commits C, D, and E were pushed by accident and need to be moved to branch-2.
The order is:
# переключаемся на нужный бранч
git checkout branch-2
# мержим бранч с коммитами
git merge branch-1
# переключаемся на бранч, в который случайно запушили
git checkout branch-1
# делает жесткий резет на 3 коммита назад (все, что мы сделали и на закомитили в ветке сотрется)
git reset --hard HEAD~3
# переключаемся на наш бранч
git checkout branch-2

Result:
branch-1 A - B
branch-2 F - C - D - E

A
alegzz, 2021-05-03
@alegzz

man git-cherry-pick

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question