git 将分支 b 的更改应用到 a,而不合并或添加提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20045946/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:19:52  来源:igfitidea点击:

Applying the changes from branch b to a, without merging or adding commits

gitmergebranch

提问by Bj?rn Lindqvist

My scenario is that I have one branch in which I've made big improvements to the build process (branch A) and in another I'm working on a unrelated feature (branch B). So now when I'm hacking away at branch B, I want to pull in the stuff I wrote in branch A because I want faster and easier builds. However, I don't want to "pollute" my branch B, just add changes from branchA to unstaged changes.

我的情况是,我有一个分支对构建过程(分支 A)进行了重大改进,而在另一个分支中,我正在研究一个不相关的功能(分支 B)。所以现在当我在分支 B 上进行黑客攻击时,我想引入我在分支 A 中编写的内容,因为我想要更快更容易的构建。但是,我不想“污染”我的分支 B,只需将分支 A 的更改添加到未暂存的更改。

What I've tried (when standing on branchB):

我尝试过的(当站在 branchB 上时):

git merge --no-commit branchA

Doesn't work because it puts you inside a merge. If it didn't, it would be perfect.

不起作用,因为它使您处于合并状态。如果没有,那就完美了。

git checkout branchA -- .

Doesn't work because it applies changes between branchA..branchB and not the changes master..branchA.

不起作用,因为它应用了 branchA..branchB 之间的更改,而不是 master..branchA 的更改。

Anything else?

还要别的吗?

Edit: Yes, changes on branch A are committed. In this example there is only one branch with build improvements, but there may be up to N branches with build improvements that I want to apply while working on a feature branch.

编辑:是的,分支 A 上的更改已提交。在此示例中,只有一个分支进行了构建改进,但可能有多达 N 个分支进行了构建改进,我想在处理功能分支时应用这些改进。

回答by guilffer

I just had to do something similar and was able to fix it by adding --squashto the merge command

我只需要做一些类似的事情,并且能够通过添加--squash到合并命令来修复它

git merge --no-commit --squash branchA
git reset HEAD # to unstage the changes

回答by Jonathan Wakely

cherry-pick -nshould do what you want, but I'm not sure why you want the build improvements as unstaged changes - that just makes several things harder (e.g. merging other changes to the modified files, or rebasing anything).

cherry-pick -n应该做你想做的,但我不知道为什么你想要构建改进作为非暂存更改 - 这只会使一些事情变得更难(例如,将其他更改合并到修改后的文件,或重新设置任何内容)。

In this example there is only one branch with build improvements, but there may be up to N branches with build improvements that I want to apply while working on a feature branch.

在此示例中,只有一个分支进行了构建改进,但可能有多达 N 个分支进行了构建改进,我想在处理功能分支时应用这些改进。

In that case I would create a new branch, C, which you merge from both A and B (and any other branches with build improvements). Commit changes on the feature branch, B, then merge them to the C branch, which now contains the build improvements and the feature branch changes, so you can test them together. If you need to make more changes do it in the appropriate branch, not C, then merge to C. So never change anything in the C branch, just use it to integrate changes from other branches.

在这种情况下,我将创建一个新分支 C,您可以将其从 A 和 B(以及任何其他具有构建改进的分支)合并。在功能分支 B 上提交更改,然后将它们合并到 C 分支,该分支现在包含构建改进和功能分支更改,因此您可以一起测试它们。如果您需要在适当的分支而不是 C 中进行更多更改,则合并到 C。因此永远不要更改 C 分支中的任何内容,只需使用它来集成来自其他分支的更改。

That means you can use all the features of Git in branch C, instead of juggling uncommitted changes in a dirty tree.

这意味着您可以在分支 C 中使用 Git 的所有功能,而不是在脏树中处理未提交的更改。

回答by Lucero

You should be able to cherry-pick the commits (with -nto avoid committing right away).

您应该能够挑选提交(-n以避免立即提交)。

回答by LeGEC

I'm not sure I understand your requirements.

我不确定我是否了解您的要求。

You can run a merge, then call git reset HEAD~1.

您可以运行合并,然后调用git reset HEAD~1.



The following sequence should replay every commit between masterand branchAon top of branchB. Commits which were already applied on branchBwill be skipped.

下面的顺序应该重播之间的每一个承诺master,并branchA之上branchB。已经应用的提交branchB将被跳过。

# start from branchA
git checkout branchA
# create a temporary branch wip
git checkout -b wip
# use rebase to replay each commit between master and wip on branchB
git rebase --onto branchB master wip

# if you want to remove all the commit history and only keep the resulting diffs,
# use git reset
git reset branchB

# change the active branch
git checkout branchB
# remove temp branch
git branch -d wip

回答by sobi3ch

I'm not 100% sure I understood it clearly, but in my case I've just created diff patch between branches and then applied this path on B branch.

我不是 100% 确定我清楚地理解它,但就我而言,我刚刚在分支之间创建了差异补丁,然后在 B 分支上应用了这条路径。

Inside branch A:

A分行内:

 git diff branchA..branchB > patch.diff
 git apply patch.diff