Ответ 1
Простой способ сделать это:
- создать и оформить ветку tmp в branch_a (
git branch tmp branch_a && git checkout tmp
) -
git reset --soft branch_b
-
git commit
этот коммит должен иметь все различия
У меня две ветки, которые имеют очень мало сходной истории, но связаны друг с другом.
Мне нужны изменения между этими двумя в один git commit.
файлы были удалены и созданы между этими патчами, и я хочу, чтобы патч отражал это.
i.e: следующий материал не будет работать:
git diff branch_a branch_b -- > patchfile
git checkout branch_b
git apply patchfile # deletes and adds are ignored
git commit # we miss the deletes
Простой способ сделать это:
git branch tmp branch_a && git checkout tmp
)git reset --soft branch_b
git commit
этот коммит должен иметь все различия
Если у вас есть две ветки:
has-changes
needs-changes
И вы хотите переместить изменения из has-changes
в needs-changes
, а затем сделать следующее:
git checkout -b deleteme has-changes # Create temporary branch to build commit on
git reset --soft needs-changes # Move diff into index
git commit # Create the diff patch commit
git checkout needs-changes
git cherry-pick deleteme # Apply the diff to the needs-changes
git branch -D deleteme # Delete the temporary branch
Если вас не волнует, что has-changes
change изменяется (или вы предпочитаете сохранять его другим способом), это можно немного упростить до:
git checkout has-changes
git reset --soft needs-changes
git commit
git checkout needs-changes
git cherry-pick has-changes
Все сводится к git reset --soft branch_b
поверх временной ветки, основанной на branch_a, и результат фиксируется обратно в branch_b.
Это пошаговая процедура:
#Start out on the branch with the code we want
git checkout branch_a
#create tmp branch same as branch_a (so that we don't change our local branch_a state during the operation)
git branch tmp
#working directory has all the code that we want, on tmp branch
git checkout tmp
# Change the branch head to the branch we want to be on. All the delta
# between the current source code and branch_b is now staged for commit
git reset --soft branch_b
# Move away from tmp, so our commit will go directly to branch_b
git checkout branch_b
# Now you can examine the proposed commit
git status
# Add the delta we want to the branch
git commit
# Sanity check that the branches have the same content now (should return an empty line)
git diff branch_A..branch_b
# Remove tmp, we don't need it anymore
git branch -D tmp
См. git apply --index
. Вот почему он ничего не делает.
Кроме того, убедитесь, что вы находитесь в корневом каталоге, поэтому .git
должен быть там.