简体   繁体   English

Git branch diff进入一个新的提交

[英]Git branch diff into one new commit

So let's say I have a main branch, we'll call 'master'. 所以,假设我有一个主分支,我们称之为'主'。 I've made a branch, called 'new-feature'. 我做了一个分支,称为“新功能”。 I've made a ton of commits in this branch so I can go back in time, but I've done quite a bit of back and forth on while developing the feature so the commit log is pretty messy. 我已经在这个分支中做了大量的提交,所以我可以回到过去,但是我在开发这个功能时已经做了很多来回,所以提交日志非常混乱。

If I were to look at git diff master..new-feature for example. 如果我要看git diff master..new-feature例如。

If I wanted to create just one new fresh commit on 'master' that includes all the changes in between the two branches, what's the most efficient way to do that? 如果我想在'master'上创建一个新的提交,其中包括两个分支之间的所有更改,那么最有效的方法是什么?

git checkout master
git merge --squash new-feature
git commit

The commit message will start out showing the entire list of commits being merged/squashed, but you can of course edit that to be whatever you want. 提交消息将开始显示正在合并/压缩的整个提交列表,但您当然可以将其编辑为您想要的任何内容。

Another option that might work better for you, would be to generate a patch file with git diff --patch > "patch filename" and then patch them into your master branch using git apply "patch file name" . 另一个可能更适合您的选项是使用git diff --patch > "patch filename"生成补丁文件,然后使用git apply "patch file name"它们修补到主分支中。

Man page for git-diff (especially "Generating patches with -p"): http://www.kernel.org/pub/software/scm/git/docs/git-diff.html git-diff的手册页(特别是“用-p生成补丁”): http//www.kernel.org/pub/software/scm/git/docs/git-diff.html

Man page for git-apply: http://www.kernel.org/pub/software/scm/git/docs/git-apply.html git-apply的手册页: http//www.kernel.org/pub/software/scm/git/docs/git-apply.html

You can use interactive rebase for this: git rebase --interactive . 您可以使用交互式rebase: git rebase --interactive Then just use the squash option. 然后只使用壁球选项。 It only uses the updates from the commit but doesn't create the commit. 它仅使用提交中的更新,但不创建提交。

Use git rebase --interactive HEAD~(however many commits to want to squash). 使用git rebase --interactive HEAD~(但是许多提交想要压缩)。 eg, git rebase --interactive HEAD~5. 例如,git rebase --interactive HEAD~5。 This will load your last 5 commits and bring up an editor window with the messages. 这将加载您的最后5次提交并打开包含消息的编辑器窗口。 Each message will have 'pick' at the beginning. 每条消息都会在开头有“选择”。 Go down to the last message, change 'pick' to 'squash' and exit. 转到最后一条消息,将'pick'改为'squash'并退出。 A new window will open asking for a commit message. 将打开一个新窗口,询问提交消息。 This will be the new commit message of the squash commits. 这将是壁球提交的新提交消息。 Exit and it will squash all the commits into one new one. 退出并将所有提交压缩为一个新提交。

There's an example here: https://ariejan.net/2011/07/05/git-squash-your-latests-commits-into-one 这里有一个例子: https//ariejan.net/2011/07/05/git-squash-your-latests-commits-into-one

You could commit everything in your feature branch, then merge into master accepting 'theirs' for all changes. 您可以在功能分支中提交所有内容,然后合并到主服务器中接受所有更改的“他们的”。

Here's a Stack Overflow question with a great answer on how to do that: Is there a "theirs" version of "git merge -s ours"? 这是一个Stack Overflow问题,如何做到这一点有一个很好的答案: 是否有“他们的”版本的“git merge -s ours”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM