git git从两个分支之间的差异创建提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17324645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git create commit from diff between two branches
提问by Alex
I have two branches which have very little similar history, but are related to each other.
我有两个分支,它们几乎没有相似的历史,但彼此相关。
I want the changes between those two in onegit commit.
我希望在一次git commit 中进行这两者之间的更改。
files have been deleted and created between those patches and I want the patch to reflect that
文件已被删除并在这些补丁之间创建,我希望补丁反映这一点
i.e.: the following stuff will not work:
即:以下内容将不起作用:
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
回答by Balog Pal
A simple way to do it is:
一个简单的方法是:
- create and checkout branch tmp at branch_a (
git branch tmp branch_a && git checkout tmp
) git reset --soft branch_b
git commit
- 在 branch_a (
git branch tmp branch_a && git checkout tmp
)创建和检出分支 tmp git reset --soft branch_b
git commit
that commit must have all the diff
该提交必须具有所有差异
回答by Cory Klein
If you have two branches:
如果你有两个分支:
has-changes
needs-changes
has-changes
needs-changes
And you want to move the changes from has-changes
onto needs-changes
, then do the following:
并且您想将更改从has-changes
on移动到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 # Switch to branch that needs changes
git cherry-pick deleteme # Apply the diff to the needs-changes
git branch -D deleteme # Delete the temporary branch
回答by AndrewSk
It all comes down to a git reset --soft branch_b
on top of a temp branch based on branch_a, and the result committed back to branch_b.
这一切都归结为git reset --soft branch_b
基于 branch_a 的临时分支之上的 a,并将结果提交回 branch_b。
This is a step-by-step walking through the process:
这是一个循序渐进的过程:
#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